我一直在努力解决这个问题,但我真的不知道发生了什么。我有一小段代码:
DiscoveredLocation.find_by_user_id(user.id, :include => [:boss_kills])这些模型包括:
DiscoveredLocation(id, user_id, boss_location_id)
BossKill(user_id, monster_id)和关联:
Monster belongs_to :boss_location
Monster has_many :boss_kills
BossKill belongs_to :user
BossKill belongs_to :monster
DiscoveredLocation belongs_to :user
DiscoveredLocation belongs_to :boss_location
DiscoveredLocation has_many :monsters, :through => :boss_location
DiscoveredLocation has_many :boss_kills, :through => :monsters当我执行find_by时,我得到了这个错误:
NoMethodError in BossesController#index
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each如果我将include选项更改为任何其他模型,比如:monster,它会工作得很好。我在很大程度上被这个问题所困扰:P。也许有人可以帮助我?:)
发布于 2010-12-28 03:48:02
DiscoveredLocation模型正在尝试通过关联定义嵌套的has- is:
DiscoveredLocation has_many :monsters, :through => :boss_location
DiscoveredLocation has_many :boss_kills, :through => :monsters我不相信ActiveRecord可以开箱即用地做到这一点。有一个nested_has_many_through插件声称支持这一点--我不知道它在这种情况下是否可以工作。
有关here和here的更多讨论,请参阅
发布于 2010-12-27 11:42:39
我可能错了,但是为了帮助你,你可以试试DiscoveredLocation.find_by_user_id(user.id, :include => [:boss_kill])
最后,您确定要将一个名为user的变量传递到控制器中吗?如果您使用的是流行的auth gem,那么可能是current_user?
发布于 2010-12-27 11:44:09
您还需要包括中间表,否则无法将boss_kills映射到DiscoveredLocations:
DiscoveredLocation.find_by_user_id(user.id, :include => [:monsters, :boss_locations,:boss_kills])另外,检查您的开发日志以查看生成的查询。如果可能,在这里发布它,这样我们就可以确定它是否缺少一个中间表。
https://stackoverflow.com/questions/4536473
复制相似问题