Chris Rericha
2006-Jul-28 13:49 UTC
[Rails] Rails not using default database value when saving model
I setup a migration with a table that has a default value and can not be null. create_table( :user ) do |t| t.column( :first_name, :string, {null => false} ) t.column( :last_name, :string, {null => false} ) t.column( :email_address, :string, {null => false} ) t.column( :last_login, :datetime, {:default => ''now()'', :null => false} ) end It creates the table. If I view it from within mysql, It has the default value. However, when I go to create a record it fails. User.create( :first_name => "first", :last_name => "last", :email_address => "email@address.com" ) I see that the sql statement rails makes sets last_login to NULL. How can I get rails to use the default value in this situation? Thanks! Chris -- Posted via http://www.ruby-forum.com/.
Dr Nic
2006-Jul-28 14:53 UTC
[Rails] Re: Rails not using default database value when saving model
> t.column( :last_login, :datetime, {:default => ''now()'', :null => > false} )You''re setting a Ruby string here; I guess the AR code doesn''t pass SQL expressions around, instead only uses raw data. So its either a fixed date object for all instances or its nothing. Your solution for today is to take responsibility for setting the date object yourself. Perhaps raise a core ticket (if Trac is working). -- Posted via http://www.ruby-forum.com/.