# Database saving empty records

**URL:** <https://www.ruby-forum.com/t/database-saving-empty-records/171857>\
**Category:** Rails\
**Created:** [July 10, 2009, 10:16am UTC](https://www.ruby-forum.com/t/database-saving-empty-records/171857 "2009-07-10T10:16:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Whiplash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e8c25b/32.png) [@Whiplash](https://www.ruby-forum.com/u/Whiplash)\
**Post date:** [July 10, 2009, 10:16am UTC](https://www.ruby-forum.com/t/database-saving-empty-records/171857/1 "2009-07-10T10:16:14Z")

</div>

I’ve seen some similar questions to this but I cannot find the answer.  
I have a simple form that is supposed to save some values into the  
database. However, when I call save, the record is saved with null  
values instead of the values provided in the form.

As you can see by the following console output, the parameters are  
being posted properly, but for some reason they are not being entered  
into the database. I’m not doing any validations or anything else. I’m  
also not receiving any errors.

What’s the deal?

Processing MotorcyclesController#create (for 127.0.0.1 at 2009-07-09  
22:27:34) [POST]  
Parameters: {“model”=\>“asdfsd”, “cc”=\>“1234”, “commit”=\>“Save”,  
“authenticity\_token”=\>“ed86db52751e22006b4af4050daa88d8b722685a”,  
“make”=\>“dfad”, “year”=\>“1234”}  
Motorcycle Create (0.3ms) INSERT INTO “motorcycles” (“model”,  
“cc”, “created\_at”, “updated\_at”, “make”, “year”) VALUES(NULL, NULL,  
‘2009-07-10 05:27:34’, ‘2009-07-10 05:27:34’, NULL, NULL)  
Rendering template within layouts/application  
Rendering motorcycles/create

---

<div class="post-metadata">

**Author:** ![Whiplash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e8c25b/32.png) [@Whiplash](https://www.ruby-forum.com/u/Whiplash)\
**Post date:** [July 10, 2009, 2:56pm UTC](https://www.ruby-forum.com/t/database-saving-empty-records/171857/2 "2009-07-10T14:56:12Z")

</div>

Quoting W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> What’s the deal?

What about attr\_protected or attr\_accessible in the model?

HTH,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![Whiplash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e8c25b/32.png) [@Whiplash](https://www.ruby-forum.com/u/Whiplash)\
**Post date:** [July 10, 2009, 3:08pm UTC](https://www.ruby-forum.com/t/database-saving-empty-records/171857/3 "2009-07-10T15:08:29Z")

</div>

On Jul 10, 6:56 am, Whiplash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> As you can see by the following console output, the parameters are  
> being posted properly, but for some reason they are not being entered  
> into the database. I’m not doing any validations or anything else. I’m  
> also not receiving any errors.
> 
> What’s the deal?

How about showing the code that isn’t working ?

Fred

---

<div class="post-metadata">

**Author:** ![Whiplash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e8c25b/32.png) [@Whiplash](https://www.ruby-forum.com/u/Whiplash)\
**Post date:** [July 10, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/database-saving-empty-records/171857/4 "2009-07-10T18:00:53Z")

</div>

On Jul 10, 4:46 pm, Whiplash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Because form\_for by default stuck your parameters in params  
[:motorcycle], but the fields you created in the other case where at  
the top level

Fred

---

<div class="post-metadata">

**Author:** ![Whiplash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e8c25b/32.png) [@Whiplash](https://www.ruby-forum.com/u/Whiplash)\
**Post date:** [July 10, 2009, 5:47pm UTC](https://www.ruby-forum.com/t/database-saving-empty-records/171857/5 "2009-07-10T17:47:27Z")

</div>

Alright I figured out how to get it to work, but I’m still not sure  
why.

First off, I was using the form\_tag method of creating forms. Then in  
my controller I was doing

@motorycle = Motorcycle.new(params[:motorcycle])  
@motorcycle.save

This did not work. What I’ve done now is change the controller to:

@motorcycle= Motorcycle.new  
@motorcycle.make = params[‘make’]  
@motorcycle.model = params[‘model’]  
@motorcycle.year = params[‘year’]  
@motorcycle.save

And that works!

So my question becoms, why do I have to manually assign the parameters  
when using form\_tag, and I don’t when using form\_for?

Thanks

Dana

---

<div class="post-metadata">

**Author:** ![Whiplash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e8c25b/32.png) [@Whiplash](https://www.ruby-forum.com/u/Whiplash)\
**Post date:** [July 10, 2009, 11:59pm UTC](https://www.ruby-forum.com/t/database-saving-empty-records/171857/6 "2009-07-10T23:59:43Z")

</div>

Thanks Fred, starting to understand now. 🙂

Dana

On Jul 10, 8:59 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
