如何在Rails中两次加入相同的2个模型? [英] How can I join the same 2 models twice in Rails?
问题描述
我有一个具有国家/地区偏好设置的应用程序.用户将具有两种国家/地区偏好设置-事件和研究.将来可能还会更多.我更倾向于使用2个表来表示这一点,而不是使用STI.我在优雅地配置Rails时遇到了一些麻烦.我可以破解它,但是我更愿意按照Rails的惯例来做到这一点.我想要的是这样的:
I have an app with country preferences. A user will have 2 types of country preferences - event and research. In the future there may be more. I was leaning more towards having 2 tables to represent this over using STI. I'm having a bit of trouble configuring Rails elegantly to do this. I could hack it but I would rather do this by Rails convention. What I want is something like this:
class User < ActiveRecord::Base
has_many event_countries, :through => :event_countries, :class_name => 'Country'
has_many research_countries, :through => :research_countries, :class_name => 'Country'
end
class EventCountry < ActiveRecord::Base
belongs_to :country
belongs_to :user
end
class ResearchCountry < ActiveRecord::Base
belongs_to :country
belongs_to :user
end
class Country < ActiveRecord::Base
...
end
这是行不通的.有了这个伪代码",没有人知道如何在Rails中实际实现吗?
This doesn't work though. Given this "pseudo code" does anyone know how to actually implement this in Rails?
推荐答案
我认为您要声明它们错误,因为这应该可以正常工作.这就是:through
指令的用途:
I think you're going about declaring them wrong, because this should work properly. That's what the :through
directive is for:
class User < ActiveRecord::Base
has_many :event_countries
has_many :countries_with_events,
:through => :event_countries,
:source => :country
has_many :research_countries
has_many :countries_with_researches,
:through => :research_countries,
:source => :country
end
class EventCountry < ActiveRecord::Base
belongs_to :country
belongs_to :user
end
class ResearchCountry < ActiveRecord::Base
belongs_to :country
belongs_to :user
end
class Country < ActiveRecord::Base
# ...
end
很多尴尬都来自您为表格选择的标签.尽管乍一看它们似乎很合理,但是使用它们的方式最终使它们变得困难.
A lot of the awkwardness comes from the labels you've chosen for the tables. Although they'd seem reasonable at first glance, the way you use them ends up making them difficult.
您可能想调用research_countries
之类的user_research_countries
,以便关系名称可以作为user.research_countries
作为:through
:
You might want to call research_countries
something like user_research_countries
so that the relationship name can be user.research_countries
as the :through
:
class User < ActiveRecord::Base
has_many :user_event_countries
has_many :event_countries,
:through => :user_event_countries,
:source => :country
has_many :user_research_countries
has_many :research_countries,
:through => :user_research_countries,
:source => :country
end
class UserEventCountry < ActiveRecord::Base
belongs_to :country
belongs_to :user
end
class UserResearchCountry < ActiveRecord::Base
belongs_to :country
belongs_to :user
end
class Country < ActiveRecord::Base
# ...
end
您可以通过在用户国家/地区关联表中添加一个包含一个或多个标志的字段来进一步重构该字段,在这种情况下,这将是研究或事件,或者以后您需要执行的任何操作:
You can refactor this even further by adding a field to the user-country association table that includes one or more flags, which in this case would be research or event or whatever you require later:
class User < ActiveRecord::Base
has_many :user_countries
has_many :event_countries,
:through => :user_countries,
:source => :country,
:conditions => { :event => true }
has_many :research_countries,
:through => :user_countries,
:source => :country,
:conditions => { :research => true }
end
class UserCountry < ActiveRecord::Base
belongs_to :country
belongs_to :user
# * column :event, :boolean
# * column :research, :boolean
end
class Country < ActiveRecord::Base
# ...
end
这篇关于如何在Rails中两次加入相同的2个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!