# My model object's attributes are not being saved

**URL:** <https://www.ruby-forum.com/t/my-model-objects-attributes-are-not-being-saved/185954>\
**Category:** Rails\
**Created:** [March 29, 2010, 5:57pm UTC](https://www.ruby-forum.com/t/my-model-objects-attributes-are-not-being-saved/185954 "2010-03-29T17:57:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BarryOg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/91b2a8/32.png) [@BarryOg](https://www.ruby-forum.com/u/BarryOg)\
**Post date:** [March 29, 2010, 5:57pm UTC](https://www.ruby-forum.com/t/my-model-objects-attributes-are-not-being-saved/185954/1 "2010-03-29T17:57:36Z")

</div>

Hello all,  
I’m into my first week of rails coding and I’m really enjoying it so  
far.  
I’ve run into a bit of an problem. I have a user model which represents  
the  
currently logged in user. I’m also creating a gas\_reading model which  
will  
be a child of the user model. When I attempt to create and save a new  
gas\_reading its saving successfully without any exceptions but its not  
storing the attributes. I’m logging the values directly before the save  
call in the create method of my controller and they’re set correctly but  
I  
can see from the mongrel output that the sql being used does not contain  
any  
values.

I’ve been searching for a similar problem on here and haven’t managed to  
find anything and I’ve tried hacking around as much as possible but not  
been  
able to make progress.

Any help would be really appreciated.

Here’s my code:

user.rb:

class User \< ActiveRecord::Base

has\_many :gas\_readings

… lots of stuff around passwords and encryption

end

gas\_reading.rb:

class GasReading \< ActiveRecord::Base  
validates\_presence\_of :value, :start\_date, :end\_date

belongs\_to :user  
attr\_accessor :value, :start\_date, :end\_date  
end

gas\_reading\_controller.rb:

# GET /gas\_readings/new

# GET /gas\_readings/new.xml

def new  
@user = User.find(Integer(session[:user\_id]))  
@gas\_reading = @user.gas\_readings.build  
end

# POST /gas\_readings

# POST /gas\_readings.xml

def create

```
logger.error "this is gas reading before #{params[:gas_reading]}"

@user = User.find(Integer(session[:user_id]))
@gas_reading = @user.gas_readings.build(params[:gas_reading])

logger.error "this is gas reading after #{@gas_reading.value} ,

```

#{@gas\_reading.start\_date} , #{@gas\_reading.end\_date}"

```
respond_to do |format|
  if @gas_reading.save
    flash[:notice] = 'GasReading was successfully created.'
    format.html { redirect_to(@gas_reading) }
  else
    format.html { render :action => "new" }
  end
end

```

end

This is the mongrel output when I create a new gas\_reading:  
Rendering gas\_readings/new  
Completed in 39ms (View: 11, DB: 7) | 200 OK  
[[http://localhost/gas\_readings/new](http://localhost/gas_readings/new)]  
SQL (0.1ms) SET NAMES ‘utf8’  
SQL (0.1ms) SET SQL\_AUTO\_IS\_NULL=0

Processing GasReadingsController#create (for 127.0.0.1 at 2010-03-29  
16:41:41) [POST]  
Parameters: {“commit”=\>“Create”,  
“authenticity\_token”=\>“RSLUS1/MA86MXtnm2RnBIh9ksD4FAzSX2NBnu80eh8s=”,  
“gas\_reading”=\>{“end\_date”=\>“03/26/2010”, “start\_date”=\>“03/02/2010”,  
“value”=\>“23423”}}  
User Columns (1.9ms) SHOW FIELDS FROM `users`  
User Load (1.0ms) SELECT \* FROM `users` WHERE (`users`.`id` = 4)  
LIMIT 1  
this is gas reading before  
start\_date03/02/2010end\_date03/26/2010value23423  
User Load (0.3ms) SELECT \* FROM `users` WHERE (`users`.`id` = 4)  
GasReading Columns (2.2ms) SHOW FIELDS FROM `gas_readings`  
this is gas reading after 23423 , 03/02/2010 , 03/26/2010  
SQL (0.7ms) BEGIN  
GasReading Create (0.3ms) INSERT INTO `gas_readings` (`start_date`,  
`end_date`, `updated_at`, `user_id`, `created_at`) VALUES(NULL, NULL,  
‘2010-03-29 15:41:41’, 4, ‘2010-03-29 15:41:41’)  
SQL (26.2ms) COMMIT  
Redirected to [http://localhost:3000/gas\_readings/9](http://localhost:3000/gas_readings/9)  
Completed in 62ms (DB: 33) | 302 Found [[http://localhost/gas\_readings](http://localhost/gas_readings)]

–  
View this message in context:  
[http://old.nabble.com/My-model-object's-attributes-are-not-being-saved-tp28071263p28071263.html](http://old.nabble.com/My-model-object%27s-attributes-are-not-being-saved-tp28071263p28071263.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![BarryOg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/91b2a8/32.png) [@BarryOg](https://www.ruby-forum.com/u/BarryOg)\
**Post date:** [March 30, 2010, 10:57am UTC](https://www.ruby-forum.com/t/my-model-objects-attributes-are-not-being-saved/185954/2 "2010-03-30T10:57:22Z")

</div>

Ah thanks for your reply. I didn’t fully understand how active  
records build attribute accessors based on table names for you. Also  
turns out I had named one of my columns value and it wasn’t being  
added by the migration.

---

<div class="post-metadata">

**Author:** ![BarryOg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/91b2a8/32.png) [@BarryOg](https://www.ruby-forum.com/u/BarryOg)\
**Post date:** [March 29, 2010, 6:28pm UTC](https://www.ruby-forum.com/t/my-model-objects-attributes-are-not-being-saved/185954/3 "2010-03-29T18:28:37Z")

</div>

On Mar 29, 10:53 am, BarryOg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello all,  
> I’m into my first week of rails coding and I’m really enjoying it so far.

Welcome!

> I’ve run into a bit of an problem. I have a user model which represents the  
> currently logged in user. I’m also creating a gas\_reading model which will  
> be a child of the user model. When I attempt to create and save a new  
> gas\_reading its saving successfully without any exceptions but its not  
> storing the attributes.

That’s because you’re not letting ActiveRecord see those attributes.  
Take out this line:

attr\_accessor :value, :start\_date, :end\_date

You should already have these columns in your table from your  
migration. attr\_accessor is when you want your object to temporarily  
keep track of data that is _not_ stored in your table.

Hope this helps,

Jeff

[purpleworkshops.com](http://purpleworkshops.com)
