Я думаю, что вы собираетесь объявить их неправильно, потому что это должно работать правильно.Вот для чего нужна директива :through
:
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
Большая неловкость исходит от ярлыков, которые вы выбрали для таблиц.Хотя на первый взгляд они кажутся разумными, их использование в конечном итоге усложняет их.
Возможно, вы захотите назвать research_countries
что-то вроде user_research_countries
, чтобы имя отношения могло быть user.research_countries
как :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
Вы можете еще больше изменить это, добавив поле в таблицу сопоставления стран-пользователей, содержащее один или несколько флагов, которые в этом случае будут исследованием или событием или чем-то ещевам потребуется позже:
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