# Collection\_select not being submitted database

**URL:** <https://www.ruby-forum.com/t/collection-select-not-being-submitted-database/192209>\
**Category:** Rails\
**Created:** [July 26, 2010, 12:48pm UTC](https://www.ruby-forum.com/t/collection-select-not-being-submitted-database/192209 "2010-07-26T12:48:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![harks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@harks](https://www.ruby-forum.com/u/harks)\
**Post date:** [July 26, 2010, 12:48pm UTC](https://www.ruby-forum.com/t/collection-select-not-being-submitted-database/192209/1 "2010-07-26T12:48:15Z")

</div>

Hi everybody,  
I apologize for my student’s english.

I got some problem with my Rails app, i got a collection\_select who  
aren’t submited & save in my database. After 2 days googling and didn’t  
find anything helpful i’am asking your help.

I had two tables: Crc and User: (I removed useless relation from others  
tables)

User model:

class User \< ActiveRecord::Base  
has\_many :crcs  
accepts\_nested\_attributes\_for :crcs  
acts\_as\_authentic

def role\_symbols  
return ["#{self.role.name}".to\_sym]  
end  
end

Crc model:  
class Crc \< ActiveRecord::Base  
belongs\_to :user  
attr\_accessible :user\_id

validates\_presence\_of :name, :place  
validates\_uniqueness\_of :name  
end

My views: (I removed user field form except username)

\<% form\_for @user do |f| %\>  
\<%= f.error\_messages %\>

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

  
  
\<%= collection\_select(:crc, :user\_id, Crc.all, :id, :name) %\>  

\<%= f.submit %\>

\<% end %\>

My logs:

Processing UsersController#create (for 127.0.0.1 at 2010-07-26 12:20:29)  
[POST]  
Parameters: {“commit”=\>“Save changes”,  
“authenticity\_token”=\>“6qJ/pJVcDVvNeaACnyOKRKT2R6EIA7TWVfvlMXxRMkk=”,  
“user”=\>{“password\_confirmation”=\>“pass”, “role\_id”=\>“1”,  
“username”=\>“usertest”, “password”=\>“pass”, “email”=\>“removed\_email\_address@domain.invalid”},  
“crc”=\>{“user\_id”=\>“2”}}

For me that seems good.

i got an User\_Create acion:

User Create (0.3ms) INSERT INTO `users` (`created_at`,  
`crypted_password`, `updated_at`, `username`, `role_id`,  
`password_salt`, `persistence_token`, `email`) VALUES(‘2010-07-26  
10:20:29’,  
‘b693ef04a548e3006ac8d81534717b46d7cb6146dd60e5f0e253d88356798431a5accabb135ee4727a68b9b9692e4245ab10ef9455fdbcfb44ffc88b5b402897’,  
‘2010-07-26 10:20:29’, ‘usertest’, 1, ‘SCgvhFqM0E9R7rkxXPWY’,  
‘26260bc8aa988fc3895af5b6cfcc5778b0872ce21aaa6924ac38f6c185293b457edb81d95a2ee3d5d7b78ea76a3229e6da68e6102c258861e857a769e784f2a5’,  
‘removed\_email\_address@domain.invalid’)

I didn’t got Crc\_Create action or any other Crc action. And my user\_id  
field still ‘NULL’ on database.

Thanks for reading, I hope is clear.

Loic/Harks

---

<div class="post-metadata">

**Author:** ![harks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@harks](https://www.ruby-forum.com/u/harks)\
**Post date:** [July 26, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/collection-select-not-being-submitted-database/192209/2 "2010-07-26T14:50:55Z")

</div>

On Jul 26, 11:48 am, Loic R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> User model:
> 
> \<% form\_for @user do |f| %\>  
> \<%= f.error\_messages %\>
> 
> \<%= f.label :username %\>  
>   
> \<%= f.text\_field :username %\>
> 
>   
> 
>   
> \<%= collection\_select(:crc, :user\_id, Crc.all, :id, :name) %\>

That’s not going to put parameters in the form expected by  
accepts\_nested\_attributes\_for (the easiest way to do that is to use  
fields\_for). You’re also not submitting name or place attributes for  
crc so the crc wouldn’t able to be saved anyway. Last of all you say  
you want tp create a new crc, but you’re asking the user to choose  
from a drop down populated with existing crcs, which doesn’t make  
sense to me

Fred

---

<div class="post-metadata">

**Author:** ![harks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@harks](https://www.ruby-forum.com/u/harks)\
**Post date:** [July 26, 2010, 3:18pm UTC](https://www.ruby-forum.com/t/collection-select-not-being-submitted-database/192209/3 "2010-07-26T15:18:56Z")

</div>

Frederick C. wrote:  
ï¿½ ï¿½ ï¿½

> That’s not going to put parameters in the form expected by  
> accepts\_nested\_attributes\_for (the easiest way to do that is to use  
> fields\_for). You’re also not submitting name or place attributes for  
> crc so the crc wouldn’t able to be saved anyway. Last of all you say  
> you want tp create a new crc, but you’re asking the user to choose  
> from a drop down populated with existing crcs, which doesn’t make  
> sense to me
> 
> Fred

Hi,  
I understand the non-sense. I used fields\_for and it’s works but as you  
say, I create a new crc when I want to edit one.

I put a partial on my users views:

```
  <% f.fields_for :crcs do |crc| %>
    </p>
      <%= render :partial => 'crcatt', :locals => { :f => crc } %>
    </p>
  <% end %>

```

and on “\_crcatt.html.erb” I got:  
\<%= f.collection\_select( :user\_id, @crcs, :id, :name) %\>

how can I edit the choosen crc ? I’am a ‘little’ bit lost… I only got  
4 month of rails experience and 1 years of OO language. Sorry for  
telling you my life ;).

Thanks for your help.

LoÃ¯c

---

<div class="post-metadata">

**Author:** ![harks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@harks](https://www.ruby-forum.com/u/harks)\
**Post date:** [July 26, 2010, 3:43pm UTC](https://www.ruby-forum.com/t/collection-select-not-being-submitted-database/192209/4 "2010-07-26T15:43:49Z")

</div>

Frederick C. wrote:

> On Jul 26, 2:18ï¿½pm, Loic R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½ ï¿½ ï¿½ \<% end %\>
> > 
> > and on “\_crcatt.html.erb” I got:  
> > ï¿½ ï¿½ ï¿½ ï¿½ \<%= ï¿½f.collection\_select( :user\_id, @crcs, :id, :name) %\>
> > 
> > how can I edit the choosen crc ? I’am a ‘little’ bit lost… I only got  
> > 4 month of rails experience and 1 years of OO language. Sorry for  
> > telling you my life ;).
> 
> What is the choosen crc ? According to what you posted above, users  
> have multiple crcs.
> 
> Fred

I mean the chosen crc on my collection.  
Exactly users have multiple crcs, this is why I used nested attribute  
with child\_link to add crcs to users. (I hope I’m understandable …)

In fact crcs are already created and had a ‘NULL’ field on DB (user\_id)  
that I want to edit on submitting with my collection.

Thanks again Fred for your help.

---

<div class="post-metadata">

**Author:** ![harks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@harks](https://www.ruby-forum.com/u/harks)\
**Post date:** [July 26, 2010, 3:26pm UTC](https://www.ruby-forum.com/t/collection-select-not-being-submitted-database/192209/5 "2010-07-26T15:26:05Z")

</div>

On Jul 26, 2:18 pm, Loic R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> <% end %>
> 
> ```
> 
> and on “\_crcatt.html.erb” I got:  
> \<%= f.collection\_select( :user\_id, @crcs, :id, :name) %\>
> 
> how can I edit the choosen crc ? I’am a ‘little’ bit lost… I only got  
> 4 month of rails experience and 1 years of OO language. Sorry for  
> telling you my life ;).

What is the choosen crc ? According to what you posted above, users  
have multiple crcs.

Fred

---

<div class="post-metadata">

**Author:** ![harks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@harks](https://www.ruby-forum.com/u/harks)\
**Post date:** [July 26, 2010, 4:06pm UTC](https://www.ruby-forum.com/t/collection-select-not-being-submitted-database/192209/6 "2010-07-26T16:06:48Z")

</div>

Frederick C. wrote:

> On Jul 26, 2:43ï¿½pm, Loic R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I mean the chosen crc on my collection.  
> > Exactly users have multiple crcs, this is why I used nested attribute  
> > with child\_link to add crcs to users. (I hope I’m understandable …)
> > 
> > In fact crcs are already created and had a ‘NULL’ field on DB (user\_id)  
> > that I want to edit on submitting with my collection.
> 
> This is a really weird way of doing things, so I’m not sure this will  
> mesh with stuff nested attributes.  
> If i were you (and had to do it this way) I’d probably just submit the  
> id of the chosen crc separately and have a line somewhere that updates  
> the user\_id for that crc. Seems rather back to front though.
> 
> Fred

Ok,I’m working on a cleanest way to do this. I already have waste a lot  
of time for this, thank for yours help I’m not going to waste my time  
any more.

Thanks a lot Fred.

---

<div class="post-metadata">

**Author:** ![harks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@harks](https://www.ruby-forum.com/u/harks)\
**Post date:** [July 26, 2010, 3:56pm UTC](https://www.ruby-forum.com/t/collection-select-not-being-submitted-database/192209/7 "2010-07-26T15:56:31Z")

</div>

On Jul 26, 2:43 pm, Loic R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I mean the chosen crc on my collection.  
> Exactly users have multiple crcs, this is why I used nested attribute  
> with child\_link to add crcs to users. (I hope I’m understandable …)
> 
> In fact crcs are already created and had a ‘NULL’ field on DB (user\_id)  
> that I want to edit on submitting with my collection.

This is a really weird way of doing things, so I’m not sure this will  
mesh with stuff nested attributes.  
If i were you (and had to do it this way) I’d probably just submit the  
id of the chosen crc separately and have a line somewhere that updates  
the user\_id for that crc. Seems rather back to front though.

Fred
