How can I join the same 2 models twice in Rails?

孤人 提交于 2019-12-13 16:08:36

问题


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

This doesn't work though. Given this "pseudo code" does anyone know how to actually implement this in Rails?


回答1:


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.

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


来源:https://stackoverflow.com/questions/4196058/how-can-i-join-the-same-2-models-twice-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!