【发布时间】:2014-09-20 21:23:27
【问题描述】:
我有这种情况:
user.rb
has_many :games
item.rb
has_many :games
game.rb
belongs_to :user, :foreign_key => 'user_id'
belongs_to :item, :foreign_key => 'item_id'
Item :
id : 13 | name : "Foo"
User :
id : 1 | name : "Me"
Game :
id : 1 | user_id : 4 | item_id : 13
id : 2 | user_id : 1 | item_id : 13
id : 3 | user_id : 2 | item_id : 2
id : 4 | user_id : 1 | item_id : 13
....
这行得通:
item=Item.find(13)
user=User.find(1)
user.games (returns all games with the user_id == 1)
item.games (return all games with item_id == 13)
但是现在我想要用户 1 和项目 13 的所有游戏,我该怎么做?这不起作用:
user.item.games
谢谢
【问题讨论】:
-
用户和物品是什么关系?请提及这三者之间的关系。
标签: ruby-on-rails ruby database associations