Mongoid - two fields inverses of the same foreign

2019-05-14 18:32发布

I'm trying to get the following Mongoid relationships to work, but the game field of each team is an empty array. Is this not a valid relational model? Do I need to split up games, i.e. home_games and away_games?

class Team
  include Mongoid::Document

  has_many :games, :autosave => true

end

class Game
  include Mongoid::Document

  belongs_to :home_team, :class_name => "Team", :inverse_of => :games
  belongs_to :away_team, :class_name => "Team", :inverse_of => :games

end

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-05-14 19:07

I dont think so there is a straight way to do this, may be you workaround by

class Team
  include Mongoid::Document

  has_many :home_played, :class_name => 'Game' , :inverse_of => :home_team
  has_many :away_played, :class_name => 'Game' , :inverse_of => :away_team


 def games
    Game.any_of({:home_team_id => self.id},{:away_team_id => self.id})
 end

end

class Game
  include Mongoid::Document

  belongs_to :home_team, :class_name => "Team", :inverse_of => :home_played
  belongs_to :away_team, :class_name => "Team", :inverse_of => :away_played

end

so now you can use it like

g = Game.new
+--------------------------+-------+--------------------------+--------------+--------------+
| _id                      | _type | _id                      | home_team_id | away_team_id |
+--------------------------+-------+--------------------------+--------------+--------------+
| 4ec76f70b356f8031f000001 |       | 4ec76f70b356f8031f000001 |              |              |
+--------------------------+-------+--------------------------+--------------+--------------+
1 row in set
>> t=Team.new
+--------------------------+-------+--------------------------+
| _id                      | _type | _id                      |
+--------------------------+-------+--------------------------+
| 4ec76f75b356f8031f000002 |       | 4ec76f75b356f8031f000002 |
+--------------------------+-------+--------------------------+
1 row in set
>> t.save
=> true
g.home_team = t
+--------------------------+-------+--------------------------+
| _id                      | _type | _id                      |
+--------------------------+-------+--------------------------+
| 4ec76f75b356f8031f000002 |       | 4ec76f75b356f8031f000002 |
+--------------------------+-------+--------------------------+
1 row in set
>> g.save
=> true

and

>> Team.first.home_played
+--------------------------+-------+--------------------------+--------------------------+--------------+
| _id                      | _type | _id                      | home_team_id             | away_team_id |
+--------------------------+-------+--------------------------+--------------------------+--------------+
| 4ec76f70b356f8031f000001 |       | 4ec76f70b356f8031f000001 | 4ec76f75b356f8031f000002 |              |
+--------------------------+-------+--------------------------+--------------------------+--------------+
1 row in set
>> Game.first.home_team
+--------------------------+-------+--------------------------+
| _id                      | _type | _id                      |
+--------------------------+-------+--------------------------+
| 4ec76f75b356f8031f000002 |       | 4ec76f75b356f8031f000002 |
+--------------------------+-------+--------------------------+

abd you can get the total count by

>> Team.first.games

Hope this helps

查看更多
登录 后发表回答