简体   繁体   English

如何在没有直接belongs_to的情况下设置一种“belongs_to:through”关联?

[英]How do I set up a kind of “belongs_to :through” association without a direct belongs_to?

I know that "belongs_to :through" is not valid. 我知道“belongs_to:through”无效。 It's just my attempt to express what I want to achieve. 这只是我试图表达我想要实现的目标。 Just bear with me for a sec... 跟我一起玩一会儿...

This is what I have: 这就是我所拥有的:

class League
  has_many :divisions
end

class Division
  belongs_to :league
  has_many :teams
end

class Team
  belongs_to :division
  has_many :players
end

class Player
  belongs_to :team
end

Now, in order to make a "baseball card" view form, I need: 现在,为了制作“棒球卡”视图表格,我需要:

name
team.name
team.division.name
team.division.league.name

So, is there a way to set up a "belongs_to :through" association to directly access 'division.name' from 'players_controller' without the 'team.' 那么,有没有办法建立一个“belongs_to:through”关联来直接从'players_controller'访问'division.name'而没有'team'。 prefix?? 字首?? I have to access a lot of columns from 'player' to 'division', so I'm looking for a way to get "direct" access to those columns. 我必须访问从'player'到'division'的很多列,所以我正在寻找一种方法来“直接”访问这些列。

One option is to include a 'division_id' column in the 'players' table, but I've been told that it would kinda break the relational data model, since it would allow for inconsistency if the data selection functionality is not properly handled (eg player A is on team A which is in division A, but player A has its division_id column set to division B). 一个选项是在'players'表中包含'division_id'列,但我被告知它会破坏关系数据模型,因为如果数据选择功能没有得到妥善处理,它将允许不一致(例如玩家A在A组,在A组,但是玩家A的division_id列设置为B组。

Is it possible to make a "symbolic link", eg 'division' now refers to 'team.division', and 'league' now refers to 'team.division.league'?? 是否有可能形成一个“象征性的联系”,例如“分裂”现在指的是'team.division','联盟'现在指的是'team.division.league'?

Or, is the only true option to use the full path each time?? 或者,每次使用完整路径是唯一真正的选择吗?

Hope someone can help. 希望有人能提供帮助。

Use delegate in the model class. 在模型类中使用delegate。

class Team < ActiveRecord::Base
  belongs_to :division
  has_many :players

  delegate :league, to: :division
end

Reference: http://api.rubyonrails.org/classes/Module.html#method-i-delegate 参考: http//api.rubyonrails.org/classes/Module.html#method-i-delegate

You may try 你可以试试

class Player
      belongs_to :team
      has_one :division, :through => :team
    end

You can define a helper method in your player model: 您可以在播放器模型中定义辅助方法:

def division
  team.division
end

def league
  team.division.league
end

Of course, this only relates to readability of your code and does not affect the form of the database queries involved. 当然,这只涉及代码的可读性,并不影响所涉及的数据库查询的形式。 If your statement generates multiple SQL queries but you want only one, check out the .include option here: Rails Guides - Active Record Query Interface 如果您的语句生成多个SQL查询但只需要一个,请在此处查看.include选项: Rails指南 - 活动记录查询接口

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

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