Forum: Ruby on Rails Convention for setting join model fields when associating

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Max W. (Guest)
on 2009-04-24 20:00
Hi folks

I have this situation:

Region
  has_many :policy_indicator_allocations, :order => "position"
  has_many :policy_indicators, :through => :policy_indicator_allocations

PolicyIndicatorAllocation
  belongs_to :region
  belongs_to :policy_indicator
  belongs_to :year

PolicyIndicator
  has_many :policy_indicator_allocations
  has_many :regions, :through => :policy_indicator_allocations

Year
  has_many :policy_indicator_allocations

So, policy_indicator_allocations are joins between a region and a policy
indicator, for a particular year.

What i'm wondering is this:  when i come to associate a policy indicator
with a region, i want to do it for a particular year.  What's the
best/most conventional way to do this?  (assume i have the relevant
models in instance vars)

A not very clever way is this:
  PolicyIndicatorAllocation.create(
    :region_id => @region.id,
    :policy_indicator_id => @policy_indicator.id,
    :year_id => @year.id )

Is there a neater way?

thanks
max
This topic is locked and can not be replied to.