On Dec 25, 2011, at 12:15 AM, Bob Smith wrote:

> 
> 
> On Dec 24, 1:18 pm, Walter Lee Davis <[email protected]> wrote:
> Use an :after_create callback in those cases; you will have the ID at
> that point and you can use it. Remember, you will need to set any
> relationship keys directly, not at the object level, since you can't
> call save again in an after_create (I don't think). Here's my
> after_save method from a similar setup:
> 
>  def set_primary
>    self.update_attributes( :role_id => self.roles.first.id ) if
> self.roles.first
>  end
> 
> 
>> This is from inside a Title, which has_many roles, has_many people through 
>> roles, and belongs_to one role (designating the "primary" person, like the 
>> author or the editor -- the one that people think of when they're looking 
>> for that book, even though lots of people may have contributed to it).
>> 
>> I needed to go this route because I was using Ryan Bates' nested_form gem, 
>> and so I was adding roles to a title that hadn't been saved yet -- very 
>> similar to your setup if I recall correctly. Since I can't designate a 
>> primary in the #new method, because nothing has anIDyet, I use this callback 
>> to sort things out, and count on my editors to always choose the most 
>> important person first. I have a new_record? test in my view to hide 
>> theradiobuttons in that case, and show them in the #edit view of the same 
>> form.
>> 
>> Walter
> 
> This is very close to what I wanted. It seems you were having the same
> problem as I am using the new_record? test to remove the
> radio_buttons. I am trying to find a way to get the id after the
> create and putting it in the Household.hoh field. Maybe in the
> after_create callback for each Person object. But how can I access the
> radio button and see who was selected from there ?? I see you
> used .first to set the id. Will this help me see who was selected by
> the radio buttons ??

No, this only hacks around the problem of setting the primary role in a new 
title object by choosing the first member of the has_many roles collection 
within the controller. The actual method of getting the role from the radio 
button is much simpler and more direct. 

        <%= radio_button_tag 'title[role_id]', f.object.id, (@title.role_id == 
f.object.id) %>

That's inside a partial called _role_fields.html.erb, and it's filled in using 
the nested_form gem as I mentioned earlier. It's just named correctly to act on 
the parent title object, and inside the partial, f.object points to the 
individual role object.

Walter

> 
> Bob
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rubyonrails-talk?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to