在after_create过滤器中的if条件中使用Proc.new

时间:2013-05-26 09:49:19

标签: ruby-on-rails ruby-on-rails-3.2

我在CodeSchools教程中遇到了以下代码。

class Following < ActiveRecord::Base

   after_create :queue_new_follower_email,
      if: Proc.new {|f| f.followed_user.receive_emails? }

 end

我很困惑。什么是f变量,它来自哪里?它是对当前模型对象的引用吗?如果是的话,我应该如何猜到它?(文档/源代码?)

我知道Proc块的语法,但我对'f'变量的来源感到困惑?

1 个答案:

答案 0 :(得分:2)

Rails将模型作为参数传递。

请参阅http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

class Firm < ActiveRecord::Base
  # Destroys the associated clients and people when the firm is destroyed
  before_destroy { |record| Person.destroy_all "firm_id = #{record.id}"   }
  before_destroy { |record| Client.destroy_all "client_of = #{record.id}" }
end