我有這種情況:Ruby on Rails的 - 雙人關聯數據庫
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
感謝
什麼是用戶和項目之間的關係?請提一下這三者之間的關係。 – 2014-09-20 21:34:50
@AaditiJain完成! – 2014-09-20 21:41:34