# Inserting multiple rows

**URL:** <https://discuss.rubyonrails.org/t/inserting-multiple-rows/3409>\
**Category:** rubyonrails-talk\
**Created:** [October 22, 2006, 1:43pm UTC](https://discuss.rubyonrails.org/t/inserting-multiple-rows/3409 "2006-10-22T13:43:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [October 22, 2006, 1:43pm UTC](https://discuss.rubyonrails.org/t/inserting-multiple-rows/3409/1 "2006-10-22T13:43:35Z")

</div>

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

Stuart

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [October 22, 2006, 3:51pm UTC](https://discuss.rubyonrails.org/t/inserting-multiple-rows/3409/2 "2006-10-22T15:51:01Z")

</div>

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:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [October 22, 2006, 5:10pm UTC](https://discuss.rubyonrails.org/t/inserting-multiple-rows/3409/3 "2006-10-22T17:10:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Long](https://avatars.discourse-cdn.com/v4/letter/l/e47774/32.png) [@Long](https://discuss.rubyonrails.org/u/Long)\
**Post date:** [October 22, 2006, 6:14pm UTC](https://discuss.rubyonrails.org/t/inserting-multiple-rows/3409/4 "2006-10-22T18:14:31Z")

</div>

Dark Ambient wrote:

> \> \> 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 \>

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.

&nbsp;&nbsp;&nbsp;&nbsp;canindustry = Foo.new &nbsp;&nbsp;&nbsp;&nbsp;canindustry.candidate\_id = current\_user.id &nbsp;&nbsp;&nbsp;&nbsp;canindustry.category\_id = x &nbsp;&nbsp;&nbsp;&nbsp;canindustry.save

Long www.edgesoft.ca

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [October 22, 2006, 6:34pm UTC](https://discuss.rubyonrails.org/t/inserting-multiple-rows/3409/5 "2006-10-22T18:34:46Z")

</div>

> Dark Ambient wrote:
> 
> > > 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
> 
> 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.

> > > ```
> > > 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
> 
> ```

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
