# What am I doing wrong in this nested form?

**URL:** <https://www.ruby-forum.com/t/what-am-i-doing-wrong-in-this-nested-form/205473>\
**Category:** Rails\
**Created:** [April 7, 2011, 9:36am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-in-this-nested-form/205473 "2011-04-07T09:36:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 7, 2011, 9:36am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-in-this-nested-form/205473/1 "2011-04-07T09:36:23Z")

</div>

I have these 2 models:

class User \< ActiveRecord::Base  
has\_one :city  
accepts\_nested\_attributes\_for :city  
end

class City \< ActiveRecord::Base  
belongs\_to :user  
end

This view:

\<%= form\_for :user,:url =\> users\_path,:method =\> :post do |f| %\>

\<%= f.fields\_for :city\_attributes do |b| %\>  
\<%= b.collection\_select :id,City.all,:id,:name %\>  
\<% end %\>

 \<%= f.label :name %\>  
 \<%= f.text\_field :name %\> 

 \<%= f.submit %\> 
\<% end %\>

So, I already have a list of cities added. I want to allow the user to  
select from one of the available cities in the select. But, when I  
submit the form, I get this error:

Couldn’t find City with ID=1 for User with ID=

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [April 7, 2011, 11:28am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-in-this-nested-form/205473/2 "2011-04-07T11:28:12Z")

</div>

On Apr 7, 8:36am, John C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This view:
> 
> \<%= form\_for :user,:url =\> users\_path,:method =\> :post do |f| %\>
> 
> \<%= f.fields\_for :city\_attributes do |b| %\>  
> \<%= b.collection\_select :id,City.all,:id,:name %\>  
> \<% end %\>

With accepts\_nested attributes this should just be f.fields\_for :city

Fred

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [April 7, 2011, 11:43am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-in-this-nested-form/205473/3 "2011-04-07T11:43:06Z")

</div>

If I put f.fields\_for :city, this gets generated:

and I get an AssociationMismatch saying  
ActiveRecord::AssociationTypeMismatch: City(#36862620) expected, got  
Hash(#21169932)

If I put f.fields\_for :city\_attributes, this gets generated:

Which looks more like what the nested form would accept.

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [April 7, 2011, 1:44pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-in-this-nested-form/205473/4 "2011-04-07T13:44:46Z")

</div>

On Apr 7, 10:43am, John C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Which looks more like what the nested form would accept. With accepts\_nested\_attributes you always just use the association name - you don’t need to mangle things. Also I’m slightly confused by your models / form. Does a city really belong to a unique user ? Fred

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [April 7, 2011, 2:37pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-in-this-nested-form/205473/5 "2011-04-07T14:37:47Z")

</div>

Ignore the model names and what they stand for :). They are only for  
example purpose.

So, if I make my view like this:

\<%= form\_for :user,:url =\> users\_path,:method =\> :post do |f| %\>

\<%= f.fields\_for :city do |b| %\>  
\<%= b.collection\_select :id,City.all,:id,:name %\>  
\<% end %\>

 \<%= f.label :name %\>  
 \<%= f.text\_field :name %\> 

 \<%= f.submit %\> 
\<% end %\>

Then I get the association mismatch:

City(#37602432) expected, got  
ActiveSupport::HashWithIndifferentAccess(#32969988)

With these parameters:

{“commit”=\>“Save User”,  
“authenticity\_token”=\>“whatever”,  
“utf8”=\>“✓”,  
“user”=\>{“city”=\>{“id”=\>“1”},  
“name”=\>“as”}}

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [April 7, 2011, 3:06pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-in-this-nested-form/205473/6 "2011-04-07T15:06:56Z")

</div>

On Apr 7, 1:37 pm, John C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ignore the model names and what they stand for :). They are only for  
> example purpose.
> 
> So, if I make my view like this:
> 
> \<%= form\_for :user,:url =\> users\_path,:method =\> :post do |f| %\>

Oh, for nested attributes to work you need an actual instance of user,  
not the symbol :user

> \<%= f.fields\_for :city do |b| %\>  
> \<%= b.collection\_select :id,City.all,:id,:name %\>  
> \<% end %\>

I’m not convinced that this will play well with nested attributes.  
nested attributes mostly wants to either create a new object or edit  
an existing one, whereas you want to choose from a list of objects and  
then edit one of those (to set the user\_id)

Fred

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [April 7, 2011, 4:14pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-in-this-nested-form/205473/7 "2011-04-07T16:14:50Z")

</div>

On Apr 7, 2:19pm, John C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I guess it’s expecting me to build the city instance in the controller?

correct. field\_for on an association that has nested attributes turned  
on renders its block for each associated object.

> The way I got it to work is by doing this in my model:
> 
> def city\_attributes=(attribs)  
> self.city = City.find(attribs[:id])  
> end
> 
> Is there no other way?

It might be easier to just have a city\_id virtual attribute rather  
than trying to bend accepts\_nested\_attributes\_for (given that you are  
currently overriding all of the stuff it gives you)

Fred

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [April 7, 2011, 3:19pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-in-this-nested-form/205473/8 "2011-04-07T15:19:33Z")

</div>

If I try to pass an User instance, the select isn’t displayed. Here’s  
the view now:

\<%= form\_for @user,:url =\> users\_path,:method =\> :post do |f| %\>

\<%= f.fields\_for :city do |b| %\>  
\<%= b.collection\_select :id,City.all,:id,:name %\>  
\<% end %\>

 \<%= f.label :name %\>  
 \<%= f.text\_field :name %\> 

 \<%= f.submit %\> 
\<% end %\>

I guess it’s expecting me to build the city instance in the controller?  
The way I got it to work is by doing this in my model:

def city\_attributes=(attribs)  
self.city = City.find(attribs[:id])  
end

Is there no other way?

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [April 7, 2011, 5:57pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-in-this-nested-form/205473/9 "2011-04-07T17:57:53Z")

</div>

But, how would having it as a virtual attribute help? It would not be  
saved to the database, so it’s value would be lost.

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [April 7, 2011, 8:59pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-in-this-nested-form/205473/10 "2011-04-07T20:59:39Z")

</div>

On Apr 7, 4:57pm, John C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But, how would having it as a virtual attribute help? It would not be  
> saved to the database, so it’s value would be lost.

because you’d define city\_id= to be basically the same as the  
city\_attributes method you have above. It’s not very different to what  
you have but allows you to get rid of the accepts\_nested\_attributes  
and the fields\_for

Fred

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [April 7, 2011, 7:08pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-in-this-nested-form/205473/11 "2011-04-07T19:08:26Z")

</div>

> **[\#16 Virtual Attributes - RailsCasts](http://railscasts.com/episodes/16-virtual-attributes)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
