On Jan 16, 2012, at 12:09 AM, Bob Smith wrote: > > On Dec 25 2011, 12:32 am, Walter Lee Davis <wa...@wdstudio.com> wrote: >> On Dec 25, 2011, at 12:15 AM, Bob Smith wrote: >> >> >> >> >> >>> On Dec 24, 1:18 pm, Walter Lee Davis <wa...@wdstudio.com> 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 >>> radiobuttonand see who was selected from there ?? I see you >>> used .first to set the id. Will this help me see who was selected by >>> theradiobuttons ?? >> >> 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 >> theradiobuttonis 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 rubyonrails-talk@googlegroups.com. >>> To unsubscribe from this group, send email to >>> rubyonrails-talk+unsubscr...@googlegroups.com. >>> For more options, visit this group >>> athttp://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > > Almost there... Thanks for the help. > > The latest problem is with the radio button using a variable in the > parent to store the selected child record id. Setting > this in the view doesn't allow new records to have an id yet. Do you > know a way of having each radio button put a one in a > variable that is located in each child record ? If so, then it should > be easy for after_create to test each child record > for that value and when it's found there should already be an id to > put in the parent variable.
If I'm understanding what you're asking, you've hit the exact problem that caused me to use the after_create method instead of creating the proper form elements in the view. I know there is probably a way to do this with the normal Rails relationships and an auto_save flag (that's off the top of my head) but I couldn't ever find a way to make it work. What I settled for in the end was a combination of Ryan Bates' nested_form gem and this after_create callback to catch the edge cases. Walter > > Thanks again > > 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 rubyonrails-talk@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscr...@googlegroups.com. > 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.