简体   繁体   English

Rails中的多级关联

[英]Multi level associations in rails

I am creating an application to track football teams through out the season. 我正在创建一个应用程序来跟踪整个赛季的足球队。 but i am stuck on the design of the database. 但我被困在数据库的设计上。 One fixture has a home team and an away team. 一个装置有一个主队和一个客队。 I have created a fixture model which has two foreign keys- home_team and away_team but I cant get the association to work right. 我创建了一个具有两个外键的夹具模型-home_team和away_team,但是我无法使关联正常工作。 any ideas? 有任何想法吗? Each fixture belongs to a league. 每个装置都属于一个联赛。

The simple answer is: 简单的答案是:

class Fixture < ActiveRecord::Base
  belongs_to :home_team, :class_name => "Team", :foreign_key => :home_team
  belongs_to :away_team, :class_name => "Team", :foreign_key => :away_team
end

class Team < ActiveRecord::Base
  has_many :fixtures
end

But this is no good if you because Team.fixtures will not work. 但这对您不利,因为Team.fixtures无法正常工作。

class Team < ActiveRecord::Base
  has_many :home_fixtures, :class_name => "Fixtures", :foreign_key => :home_team
  has_many :away_fixtures, :class_name => "Fixtures", :foreign_key => :away_team
end

will give you two collections… but aggregating them will have to happen in ruby, which will be icky. 将为您提供两个集合…但是将它们汇总起来必须使用红宝石进行,这非常危险。

class Team < ActiveRecord::Base
  def fixtures(*args)
    home_fixtures.all(*args) + away_fixtures.all(*args)
  end
end

This has it's problems too, sort and limit will be all ballsed up (heh, a pun, who knew?). 这也有问题,排序和限制都将增加(嘿,双关语,谁知道?)。

class Team < ActiveRecord::Base
  has_many :fixtures, :finder_sql => 'SELECT * FROM fixtures where (home_team = #{id} or away_team = #{id})'
  has_many :home_fixtures, :class_name => "Fixtures", :foreign_key => :home_team
  has_many :away_fixtures, :class_name => "Fixtures", :foreign_key => :away_team
end

This is ugly, but may just work. 这很丑陋,但可能会起作用。 The finder_sql seems to do what's needed. finder_sql似乎可以完成所需的工作。

The other option is to use a named_scope: 另一个选择是使用named_scope:

class Fixture < ActiveRecord::Base
  named_scope :for_team_id, lambda{|team_id| {:conditions => ['(home_team = ? or away_team = ?)', team_id, team_id]} }
  belongs_to :home_team, :class_name => "Team", :foreign_key => :home_team
  belongs_to :away_team, :class_name => "Team", :foreign_key => :away_team
end

class Team < ActiveRecord::Base
  def fixtures
    Fixtures.for_team_id(id)
  end
end

This last solution is the one I'd stick with, because it's a scope it will behave much like a read only association, and prevents the :finder_sql wackiness that may happen further down the line (I mean really? How does it know how to merge more conditions? It sometimes does a subquery, a subquery? Thats just not needed here? ). 最后一个解决方案是我坚持使用的解决方案,因为它的作用域非常类似于只读关联,并防止了:finder_sql可能在以后发生的:finder_sql行为(我的意思是真的吗?它如何知道如何合并更多条件吗?有时它需要一个子查询,一个子查询吗?

Hope this helps. 希望这可以帮助。

Say that you have a Team model and a Something Model, the latter will have home_team_id and away_team_id . 假设您有一个Team模型和一个Something模型,后者将具有home_team_idaway_team_id

class Something < ActiveRecord::Base
  belongs_to :home_team, :class_name => 'Team'
  belongs_to :away_team, :class_name => 'Team'

You will refer to them as something.away_team and something.home_team . 您将它们称为something.away_teamsomething.home_team

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM