Mongoid-同一异域的两个域逆 [英] Mongoid - two fields inverses of the same foreign field
本文介绍了Mongoid-同一异域的两个域逆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使以下Mongoid关系起作用,但是每个团队的游戏领域都是一个空数组.这不是有效的关系模型吗?我需要拆分游戏,即home_games和away_games吗?
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
推荐答案
我不认为有直接的方法可以做到这一点,也许您可以通过以下方法解决此问题
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
和
>> 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
希望这会有所帮助
这篇关于Mongoid-同一异域的两个域逆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文