On Dec 24, 2011, at 2:18 AM, Bob S. wrote:
Show us how you are setting the field, presumably increatein the controller.
no way to allow only one selection.
Too many question marks.
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:
self.update_attributes( :role_id => self.roles.first.id ) if
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 B.’ 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 an ID yet, 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 the radio buttons in that case, and show them in the #edit
view of the same form.