mardi 28 juillet 2015

before_create is not called on model.create

I have thiw code:

class Project < ActiveRecord::Base
    acts_as_paranoid

    belongs_to :user
    belongs_to :organization
    accepts_nested_attributes_for :organization


    attr_accessible :name, :permalink, :organization_id, :user_id

    validates_length_of :name, :minimum => 4
    validates_presence_of   :permalink
    validates_uniqueness_of :permalink, :case_sensitive => false, :scope => :deleted_at


    validates_presence_of :user        
    validates_presence_of :organization

    before_create :generate_permalink

    protected

    def generate_permalink
      binding.pry
      self.permalink = "123456789"
    end    

end

When I call in ProjectsController#create

p = Project.new
p.name = "abcdef"
p.save

App doesn't stop on binding.pry in generate_permalink, and the project is not valid and is not saved because permalink == nil. Why generate_permalink method is not called?

Thanks

Aucun commentaire:

Enregistrer un commentaire