# Inserting multiple rows

**URL:** <https://www.ruby-forum.com/t/inserting-multiple-rows/73175>\
**Category:** Rails\
**Created:** [October 22, 2006, 3:51pm UTC](https://www.ruby-forum.com/t/inserting-multiple-rows/73175 "2006-10-22T15:51:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 22, 2006, 3:51pm UTC](https://www.ruby-forum.com/t/inserting-multiple-rows/73175/1 "2006-10-22T15:51:35Z")

</div>

Form collects multiple parameters from a select list (multiple enabled)  
what kind of code do i need in the controller ?

Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 22, 2006, 5:53pm UTC](https://www.ruby-forum.com/t/inserting-multiple-rows/73175/2 "2006-10-22T17:53:48Z")

</div>

On 10/22/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Form collects multiple parameters from a select list (multiple enabled)  
> what kind of code do i need in the controller ?
> 
> Stuart

Not making much progress here , so let me give more detail:

Form has this element -  
select name=canindustry[category\_id][] size=“6” multiple=“multiple”\>  
\<%= options\_from\_collection\_for\_select @categories, :id, :name %\>

Params being submitted (example)  
_Parameters_: {“commit”=\>“Create”, “canindustry”=\>{“category\_id”=\>[“2”,  
“3”,  
“4”, “5”]},

The fields in the canindustry model are: id | candidate\_id |  
category\_id  
I want to insert multiple rows , so using above parameters

id | candidate\_id | category\_id |  
1 11 2  
2 11 3  
3 11 4  
4 11 5

Problem is in my controller where i"ve tried a few different ways with  
no  
success:  
Right now I have this in the create action:

@canindustry.candidate\_id = current\_user.id

```
a = (params[:canindustry])
a.each {|x| a.canindustry.save

```

}

and getting back an error -

“You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.candidate\_id=”

???  
Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 22, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/inserting-multiple-rows/73175/3 "2006-10-22T19:12:12Z")

</div>

Not much around on this googling. Anyone ever do this before ?

On 10/22/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The fields in the canindustry model are: id | candidate\_id |  
> success:  
> “You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.candidate\_id=”
> 
> ???  
> Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 22, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/inserting-multiple-rows/73175/4 "2006-10-22T20:30:38Z")

</div>

Dark A. wrote:

> > “3”, “4”, “5”]},
> > 
> > Problem is in my controller where i"ve tried a few different ways with no  
> > success:  
> > Right now I have this in the create action:
> > 
> > @canindustry.candidate\_id = current\_user.id

I assume you set @canindustry somewhere to a proper object, but  
the error stated otherwise.

> > ```
> > a = (params[:canindustry])
> > a.each {|x| a.canindustry.save
> > 
> > ```
> > 
> > }

Here I think params[:canindustry][:category\_id] will give you the array  
[] of selected category ids.  
Each ‘a’ element is a String, which dnu #canindustry let alone #save.

Seems to me you need something like the following in your a.each block.

```
canindustry = Foo.new
canindustry.candidate_id = current_user.id
canindustry.category_id = x
canindustry.save

```

Long  
www.edgesoft.ca

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 22, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/inserting-multiple-rows/73175/5 "2006-10-22T20:44:53Z")

</div>

On 10/22/06, Long [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > 1 11 2
> 
> I assume you set @canindustry somewhere to a proper object, but  
> the error stated otherwise.

Yeah , it was set up as a proper object though I was getting all sorts  
of  
errors.

> ```
> canindustry = Foo.new
> canindustry.candidate_id = current_user.id
> canindustry.category_id = x
> canindustry.save
> 
> ```

Yep , it works ! Thank you!

a = (params[:canindustry][:category\_id])  
a.each {|x|  
canindustry = Canindustry.new  
canindustry.candidate\_id = current\_user.id  
canindustry.category\_id = x  
canindustry.save }

Let this thread serve as a warning jumping to fast into Rails without  
having  
Ruby array methods burned deeply in your brain.

Stuart

> 

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...
