rails before_create not trigering方法

时间:2022-12-18 01:18:56

I'm trying to trigger a method right before saving an instance. I've got the User model:

我正在尝试在保存实例之前触发方法。我有User模型:

class User < ActiveRecord::Base

    has_secure_password
    attr_accessible :name, :first_surname,:second_surname,:email, :password, :password_confirmation,:number,:credit

    before_save{ self.email.downcase! }
    before_create :generate_auth_token

    default_scope order: 'users.created_at ASC'

    has_many :operations

    def consume(what,price,agent)
        self.operations.create(category:what,price:price, agent_id:agent)
    end
end

And each User has many Operation(note the use of the pry debuger via binding.pry:

每个用户都有很多操作(注意通过binding.pry使用pry debuger:

class Operation < ActiveRecord::Base
  attr_accessible :agent_id, :comment, :postcredit, :precredit, :category, :user_id,:price
  validates_presence_of :user_id
  validates_presence_of :agent_id
  validates_presence_of :price
  validates_presence_of :precredit
  validates_presence_of :postcredit
  validates_presence_of :category
  #before_save :compute_prices, :on => :create
  before_create :compute_prices
  belongs_to :user

  private

  def compute_prices
      binding.pry
      user=User.find(self.user_id)
      self.precredit=user.credit
      #select whether adding or subtracting
      if self.category == 'credit'
          self.postcredit=self.precredit+self.price
      else
          self.postcredit=self.precredit-self.price
      end
      user.update_attributes(credit:self.postcredit)
  end
end

I populate the database with users and operations, and test it via the console $rails c --sandbox. Then I:

我使用用户和操作填充数据库,并通过控制台$ rails c --sandbox进行测试。然后我:

>fi=User.first
>ope=fi.operations.create(category:'credit',price:12.2,agent_id:3)
#Now here the debugger should start and does not

I try it with both before_create and before_save, but none work.

我尝试使用before_create和before_save,但没有一个工作。

before_create :compute_prices
before_save :compute_prices, :on => :create

The only option that worked is after_initialize :compute_prices, but this gets triggered after every find or initilialization.

唯一有效的选项是after_initialize:compute_prices,但每次查找或初始化后都会触发此选项。

Any ideas?

有任何想法吗?

SOLUTION

As explained as a comment to the first answer, the solution was to user before_validation (function), on: :create, instead of before_save ....

正如对第一个答案的评论所解释的那样,解决方案是使用before_validation(function),on :: create,而不是before_save ....

1 个解决方案

#1


17  

Is your operation valid? The callback lifecycle is here: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html and if validation fails, it won't get to the create callbacks

你的手术有效吗?回调生命周期在这里:http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html,如果验证失败,它将无法进入创建回调

#1


17  

Is your operation valid? The callback lifecycle is here: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html and if validation fails, it won't get to the create callbacks

你的手术有效吗?回调生命周期在这里:http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html,如果验证失败,它将无法进入创建回调