# Saving a new object n times to the DB

**URL:** <https://www.ruby-forum.com/t/saving-a-new-object-n-times-to-the-db/132404>\
**Category:** Rails\
**Created:** [March 19, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/saving-a-new-object-n-times-to-the-db/132404 "2008-03-19T19:44:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 19, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/saving-a-new-object-n-times-to-the-db/132404/1 "2008-03-19T19:44:45Z")

</div>

Hello,

I have a generate method that generate PIN codes and serial numbers n  
times,  
n comes from a user-submitted form, something like that:

\<% form\_for :card, :url =\> {:action =\> “generate”} do |f| -%\>

Number of cards  
\<%= f.text\_field :number\_of\_cards, :size =\> 10 %\>

\<%= submit\_tag ‘Generate’ %\>  
\<% end -%\>

In the controller I have:

def generate  
params[:number\_of\_cards].to\_i.times do  
pin = rand(999999999999).to\_s.center(12, rand(9).to\_s)  
serial = rand(999999999999).to\_s.center(12, rand(9).to\_s)  
Card.create({:pin =\> pin, :serial =\> serial})  
end  
end

Everything looks OK, but when I put a number and submit the form I got  
nothing! No error messages and no records are saved to the database !!!  
When I for example put :  
3.times do  
… etc

3 new records are successfully created in the database !

what’s the problem? Maybe params[:number\_of\_cards] doesn’t get the value  
from the form? Why? And if so why don’t I get error about some nil  
value?

Your help is really appreciated.

Regards

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 19, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/saving-a-new-object-n-times-to-the-db/132404/2 "2008-03-19T20:10:13Z")

</div>

On 19 Mar 2008, at 18:44, AN@S wrote:

> 3 new records are successfully created in the database !
> 
> what’s the problem? Maybe params[:number\_of\_cards] doesn’t get the  
> value from the form? Why? And if so why don’t I get error about some  
> nil value?

Because the parameter is being submitted as params[:card]  
[:number\_of\_cards] (that’s what form\_for does). You can see this in  
your logs. You get no error because nil.to\_i is 0

Fred

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 19, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/saving-a-new-object-n-times-to-the-db/132404/3 "2008-03-19T20:23:48Z")

</div>

On Mar 19, 2008, at 2:44 PM, AN@S wrote:

> \<% end -%\>
> 
> value from the form? Why? And if so why don’t I get error about some  
> nil value?
> 
> Your help is really appreciated.
> 
> Regards

You have form\_for :card and then f.text\_field :number\_of\_cards, so  
you’ll get a params[:card][:number\_of\_cards]

Your params[:number\_of\_cards] is therefore almost certainly nil and  
nil.to\_i == 0

It can help tremendously to put:  
\<%= debug(params) %\>

Into the view that comes back so you can see what really happens (or  
just go look in the log/development.log where you should also see the  
params hash).

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 20, 2008, 10:59am UTC](https://www.ruby-forum.com/t/saving-a-new-object-n-times-to-the-db/132404/4 "2008-03-20T10:59:20Z")

</div>

Thanks Frederick and Rob, now it works 🙂  
This debug option is really useful I didn’t know about it before\>

Thanks

On 19/03/2008, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> %\>
> 
>   
> Card.create({:pin =\> pin, :serial =\> serial})
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid
> 
> >

–  
Anas Marrawi  
Visit me at: [www.anasonline.net](http://www.anasonline.net)
