Rails: Setting attribute in after_create

activerecord, callback, ruby-on-rails

Solution

before_create is called before Base.save, since your not saving its not getting called.

Edit:

class Product < ActiveRecord::Base
   before_create :set_locale
   def set_locale
      self.locale = I18n.locale
   end
end

With this in your controller will work how you want it to.

@product = Product.create # before_create will be called and locale will be set for the new product

Problem

I would like ActiveRecord to set some DB field automatically using callbacks. ``` class Product < ActiveRecord::Base after_create :set_locale def set_locale self.locale = I18n.locale end end ``` In ./script/console I do ``` p = Product.create p ``` Field p.locale is not set. What did I do wrong?

Original source