Any way to use a radio button attached to record id during a create

I’m doing a list of family members, each with a radio button for head
of household. After the household record and the records for each
person are created, it is easy to set the hoh field to the person
wanted. The problem comes if I try to set this field before the
create. Then the field is 0, and this isn’t updated after the record
is created. Any way to fix this? By the way, I’m using rails 2.3.9
until I have these problems worked out to get a clean app to upgrade…

Thanks

Bob [email protected]

On 20 December 2011 07:03, Bob S. [email protected] wrote:

I’m doing a list of family members, each with a radio button for head
of household. After the household record and the records for each
person are created, it is easy to set the hoh field to the person
wanted. The problem comes if I try to set this field before the
create. Then the field is 0, and this isn’t updated after the record
is created.

Show us how you are setting the field, presumably in create in the
controller.

Colin

On Dec 20, 3:53am, Colin L. [email protected] wrote:

Colin

I was using a variable called hoh in the household (master) record.
This was holding the id number of the person (child) record selected.
This worked fine for existing records, but got lost with records
created at the time of the save, as these records had no id yet.

Someone else suggested that I put the hoh variable in the person
records, but if I do this, each radio button is separate, and there is
no way to allow only one selection.

It seems to me that there must be a way to do this with Active Record
Callbacks. After the record is created, it has an id. Couldn’t I test
each person record after create, and if it has the radio button
selected, pass the id to the Household.hoh variable…

But for this… Is there a way to tell if the radio button is selected
if it points to a variable somewhere else ?? Or is there a way to get
Rails to just do this at the right time the way it does with parent/
child records, setting all id’s and parent_id’s ??

Too many question marks.

Bob

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:

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 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.

Walter

On 24 December 2011 07:18, Bob S. [email protected] wrote:

Show us how you are setting the field, presumably increatein the controller.
no way to allow only one selection.

It seems to me that there must be a way to do this with Active Record
Callbacks. After the record is created, it has an id. Couldn’t I test
each person record after create, and if it has the radio button
selected, pass the id to the Household.hoh variable…

But for this… Is there a way to tell if the radio button is selected
if it points to a variable somewhere else ?? Or is there a way to get
Rails to just do this at the right time the way it does with parent/
child records, setting all id’s and parent_id’s ??

I am sorry but I have not much idea of what you are talking about.
The first thing to do is to split up the problem into the Model, View
Controller paradigm. You are mixing them up talking about things like
ActiveRecord callbacks looking at radio buttons for example. Radio
buttons are meaningless when you are talking about database records,
they are only meaningful in views and controllers.

So the first question to consider is what data do you need in the
database and what are the relationships between the models (has_many,
belongs_to etc). Only when you have got that straight then worry
about how to implement the views and controllers to give the user
interface that you want.

So do you know what is in the database and what the relationships are?
If so give us that information and then explain what the problem is.

Colin

On Dec 24, 1:18pm, Walter Lee D. [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 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 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 ??

Bob

On Dec 25 2011, 12:32am, Walter Lee D. [email protected] wrote:

call save again in an after_create (I don’t think). Here’s my

Bob


You received this message because you are subscribed to the Google G.
“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
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.

Thanks again

Bob

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

self.update_attributes( :role_id => self.roles.first.id ) if
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

On Jan 16, 12:55pm, Walter Lee D. [email protected] wrote:

To post to this group, send email to [email protected].
be easy for after_create to test each child record


You received this message because you are subscribed to the Google G.
“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
athttp://groups.google.com/group/rubyonrails-talk?hl=en.

Is there a way to have a radio button inside a partial affecting a
variable from that record so that each record has a variable with one
instance set to 1 ??
I can’t find a way to do this… First step toward what I posted about
last time…

Thanks
Bob

On Jan 16, 2012, at 12:09 AM, Bob S. wrote:

that point and you can use it. Remember, you will need to set any

used .first to set the id. Will this help me see who was selected by

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 B.’
nested_form gem and this after_create callback to catch the edge cases.

Walter