Christopher J. Bottaro
2007-May-25 05:45 UTC
default column values and creating new rows with ActiveRecord
Hello, I have a table that looks kind of like this: CREATE TABLE blahs ( id SERIAL PRIMARY KEY, ... modified_at DATETIME NOT NULL DEFAULT now() ) When I try to create a new row using ActiveRecord, it bombs saying I''m not allowed to set modified_at to NULL. new = Blah.new new.some_field = some_value new.save! # ERROR: null value in column "modified_at" violates not- null constraint (ActiveRecord::StatementInvalid) Is there any way to let ActiveRecord use the default value set at the database level? I''m using Postgres 8.2. Thanks for the help. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jacob Atzen
2007-May-25 08:11 UTC
Re: default column values and creating new rows with ActiveRecord
Christopher J. Bottaro wrote:> Hello, > I have a table that looks kind of like this: > CREATE TABLE blahs ( > id SERIAL PRIMARY KEY, > ... > modified_at DATETIME NOT NULL DEFAULT now() > ) > > When I try to create a new row using ActiveRecord, it bombs saying I''m > not allowed to set modified_at to NULL. > > new = Blah.new > new.some_field = some_value > new.save! # ERROR: null value in column "modified_at" violates not- > null constraint (ActiveRecord::StatementInvalid) > > Is there any way to let ActiveRecord use the default value set at the > database level? > > I''m using Postgres 8.2.I''m not sure if you can use the default from the database. But why are you using modified_at instead of updated_at which Rails will handle for you automatically? Also, if you want to set a default value you could simply do so in a before_save hook: def before_save self.modified_at = Time.now end -- Cheers, - Jacob Atzen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Gokhan Arli
2007-May-25 13:56 UTC
Re: default column values and creating new rows with ActiveR
Instead of modified_at you can use updated_at as column name, rails takes care of it internally. of as Jacob before_save will work, just check if the value is blank? first. Gokhan www.sylow.net Jacob Atzen wrote:> Christopher J. Bottaro wrote: >> >> new = Blah.new >> new.some_field = some_value >> new.save! # ERROR: null value in column "modified_at" violates not- >> null constraint (ActiveRecord::StatementInvalid) >> >> Is there any way to let ActiveRecord use the default value set at the >> database level? >> >> I''m using Postgres 8.2. > > I''m not sure if you can use the default from the database. But why are > you using modified_at instead of updated_at which Rails will handle for > you automatically? > > Also, if you want to set a default value you could simply do so in a > before_save hook: > > def before_save > self.modified_at = Time.now > end > > -- > Cheers, > - Jacob Atzen-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---