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\] 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 Completed in 62ms (DB: 33) | 302 Found [http://localhost/gas_readings\]