Project Ramon

A learning journey from a Ruby noob perspective

Exploring Rails: accepts_nested_attributes_for pt[2]

accepts_nested_attributes_for3_header_img

Hello and welcome!

In my last post, we were working on getting deeply nested forms to work without error.

Here’s a look at the error we ended on last time:

accepts_nested_attributes_for2_img19

I was able to get these errors to quiet down by being a bit more explicit with my two has_many associations on Student & TeacherAid by passing a parameter called inverse_of like so:

accepts_nested_attributes_for3_img2

Using inverse_of

I found some documentation here from the Rails API to give us a bit of understanding about what this options provides us.

accepts_nested_attributes_for3_img3

And here’s a snippet from a this StackOverflow question as well:

accepts_nested_attributes_for3_img4

If I were asked to boil these down, I would say that inverse_of is used for explicitness, taking the guess-work off the shoulders of Rails needing to figure which class name an association is defined between.

With this error now silent, lets take a look at our form template again.

An (almost) Working Deeply Nested Form

Here we can see that all of our form fields are properly showing up.

accepts_nested_attributes_of3_img5

Note the values for the teacher’s primary_phone and secondary_phone form fields.

accepts_nested_attributes_for3_img6

Now lets commit this form and take a look at the primary and secondary phone numbers:

accepts_nested_attributes_for3_img7

Defining the Problem

So did you catch the primary and secondary phone issue from the show template shown above? Basically I have defined two different object associations on one polymorphic table. You can see this on lines: 11 & 12 in the following illustration:

But for reasons I have yet to find insight into, both Teacher‘s and TeacherAid‘s secondary phone numbers are reading the value entered into the primary number’s field.

Lets take a quick look at our _form.html.erb and our show.html.erb templates to see if I made a mistake assigning variables to POST or GET these resource representations.

Here’s our form template:

accepts_nested_attributes_for3_img8

This appears to be correct. I’m using the f iterative variable on lines: 13-16 for the first set of phone numbers because they represent our parent model’s phone attributes. And on line: 21 I’m defining a nested iterative variable resource by using f.fields_for :teacher_aids and calling teach_aid.fields_for :primary_phone/:secondary_phone.

Lets peak at the show template to see if I made an error on that page:

accepts_nested_attributes_for3_img9

And things appear to be written correctly on this template as well.

I’m calling @teacher.primary_phone.area_code @teacher.primary_phone.number and for the TeacherAid, I’m doing the same thing with teacher_aid as well.

Next Steps

Over this coming weekend, I plan to take a closer look into polymorphic tables. My goals are to find out if I cannot, in fact, define two unique objects on one polymorphic table. This doesn’t seem likely, but it will feel good to dig into some docs and blogs to see what I’m overlooking.

Have a great weekend, and stay tuned…

Advertisements

Categories: Newbie, Ruby on rails

Tags:

2 replies

Trackbacks

  1. Exploring Rails: Polymorphic Mayhem & Accepts Nested Attribute Success | Project Ramon
  2. Exploring Rails: Nested fields_for | Project Ramon

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s