Rails not adding model field to insert query
I've added a colum开发者_运维百科n to a db in rails via a migration, and updated new.html.erb to add the field to the form.
The correct data is in the request params, but the insert query that is generated has a null for the column every time.
Here are the request params:
{"commit"=>"Create",
"assignment"=>{"start_date"=>"08/04/2010",
"project_id"=>"5",
"allotment"=>"50",
"person_id"=>"33",
"end_date"=>"08/05/2010"},
"authenticity_token"=>"8f157a2a220caf716607162ce9557ab6505aab1a"}
and here is the resulting error with query showing the null column:
Mysql::Error: Column 'end_date' cannot be null:
INSERT INTO `assignments` (`start_date`, `created_at`, `project_id`, `updated_at`, `allotment`, `person_id`, `end_date`)
VALUES('2010-08-04', '2010-08-04 03:36:21', 5, '2010-08-04 03:36:21', 50, 33, NULL)
Here's the migration in question:
class ChangeDates < ActiveRecord::Migration
def self.up
add_column :assignments, :end_date, :date, :null => false
rename_column :assignments, :date, :start_date
end
def self.down
remove_column :assignments, :end_date
rename_column :assignments, :start_date, :date
end
end
I can't figure out what went wrong. I've rolled back and migrated the change a couple of times with no luck. I'm stumped.
To answer you question about attr_accessor, scope will call the accessors method before the class method, so were saving the value to the accessor, but when the save was called it was returning the method call. I hope this clarifies it by example: http://codepad.org/gAppUMc4
I've done the same thing several times. Its super frustrating.
If you haven't already, try restarting your webserver. Rails might be caching the column names.
What version of Rails and what webserver are you running?
精彩评论