Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Help with <<, has_many :through, and intermediate attributes
3 Posts
Help with <<, has_many :through, and intermediate attributes

Thanks Adam. That is mildly frustrating, especially since has_many :through is such a common association. I'm thinking that using after_save would be less efficient in my case. I need to be able to set a single join model attribute based on a form drop down selection. As such, wouldn't it be more efficient to just create the join model records manually? Or maybe a before_save instead. I'm not sure which would result in the fewest database transaction.

I have yet to find a good, full example on best practices when using a has_many :through association. For example, I'm still a bit uncertain about validation (record uniqueness), how best to set up the form for adding/editing, etc. I'm working on writing the code to handle the following, but am sure there are best practices or good working examples for these that I'm just not aware of.

Anyhow, I'm going to go toy around with this a bit, but below is what I'm trying to accomplish. I'll post more of the code as I finish rewriting it. Thanks.

Have a form with single-select drop downs for software and software status and a multi-select box for systems the software is on.

On submit, add selected software to the current ticket (through the HABTM association)

On submit, add selected software to the selected system(s) (through the HM:T association)

Force uniqueness on (#2 and #3) in the database (so not using :uniq), using flash[:notice] to notify user

Setup the add/edit form in a logical way.

Allow for editing existing software/system and software/ticket records

Allow for deleting existing software/system and software/ticket records


By intermediate attributes you mean attributes on the join model? If so, then the post you read is correct. There is no way to directly set attributes on the join model when you use << with a has_many :through association. This is indeed frustrating. The solution depends on your domain model and what you want to set, but you can often get the behavior you want by using after_save or after_create. Rails creates the join model when saving the association, at which point you can modify the join model as you see fit.

If after_save doesn't work for you, then you can create the join models explicitly, as you said.

I searched for hours for a way to use << with a has_many :through association and set the intermediate attribute, until I came across a post that said setting intermediate attributes is not possible with <<< as a result of the has_many :through association, that it would defeat the whole purpose of having a has_many :through (intermediate attributes) if << didn't allow them to be set.

Am I missing something? What is my best option? Should I just create an "add" method that simply creates a new join model record w/ the necessary values set? How can I do something like this?

@system.software << @software, {:status_id => '1'}

3 Posts
Login to add your message