# Has Many Relationship: collection\_ids= Method Useless?

**URL:** <https://www.ruby-forum.com/t/has-many-relationship-collection-ids-method-useless/178963>\
**Category:** Rails\
**Created:** [November 14, 2009, 10:38am UTC](https://www.ruby-forum.com/t/has-many-relationship-collection-ids-method-useless/178963 "2009-11-14T10:38:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [November 14, 2009, 10:38am UTC](https://www.ruby-forum.com/t/has-many-relationship-collection-ids-method-useless/178963/1 "2009-11-14T10:38:51Z")

</div>

When modifying a model that has a has\_one or has\_many relationship  
people usually create form that will submit the id of the selected  
associated object. Rails takes this into consideration with  
validations: validate\_presence\_of :country will validate if country  
or country\_id has been set.

In Rails, providing the associated ID works fine with has\_one but if  
you try to do this with a has\_many:

form\_for(@country) do |form|  
form.select :state\_ids, @states  
form.select :state\_ids, @states  
#etc…

The selected state\_ids are written to the DB as soon as they’re  
assigned. What’s the reasoning behind this?  
How is one supposed to assign multiple collection ids via a form?

accepts\_nested\_attributes\_for doesn’t work out too well either because  
you’re just setting IDs -a field that is explicitly ignored by  
accepts\_nested except when testing whether or not the given params are  
new.

---

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [November 14, 2009, 11:44am UTC](https://www.ruby-forum.com/t/has-many-relationship-collection-ids-method-useless/178963/2 "2009-11-14T11:44:23Z")

</div>

On Sat, Nov 14, 2009 at 6:37 AM, MaggotChild [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> form\_for(@country) do |form|  
> form.select :state\_ids, @states  
> form.select :state\_ids, @states  
> #etc…
> 
> The selected state\_ids are written to the DB as soon as they’re  
> assigned. What’s the reasoning behind this?  
> How is one supposed to assign multiple collection ids via a form?  
> You have to send them as an array, and assign them as an array too.

For example,  
View  
form.select(“state\_ids[]”, @states)  
form.select(“state\_ids[]”, @states)

Controller  
@whatever.state\_ids = params[:state\_ids]

You’ll have to figure out the exact syntax, but that’s the idea.

## Hope it helps.

Leonardo M…  
There’s no place like ~

---

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [November 14, 2009, 4:51pm UTC](https://www.ruby-forum.com/t/has-many-relationship-collection-ids-method-useless/178963/3 "2009-11-14T16:51:51Z")

</div>

Leonardo M. wrote:

> On Sat, Nov 14, 2009 at 6:37 AM, MaggotChild [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > form\_for(@country) do |form|  
> > ï¿½form.select :state\_ids, @states  
> > ï¿½form.select :state\_ids, @states  
> > ï¿½#etc…
> > 
> > The selected state\_ids are written to the DB as soon as they’re  
> > assigned. What’s the reasoning behind this?  
> > How is one supposed to assign multiple collection ids via a form?  
> > You have to send them as an array, and assign them as an array too.
> 
> For example,  
> View  
> form.select(“state\_ids[]”, @states)  
> form.select(“state\_ids[]”, @states)

Leonardo is right. However, you probably don’t want several s  
for the same field – try a .

> Controller  
> @whatever.state\_ids = params[:state\_ids]
> 
> You’ll have to figure out the exact syntax, but that’s the idea.
> 
> ## Hope it helps.
> 
> Leonardo M…  
> There’s no place like ~  
> Best,  
> –  
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid
