2013-05-26 22 views
1

我在CodeSchools教程中遇到以下代码。在after_create过滤条件中使用Proc.new if条件

class Following < ActiveRecord::Base 

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

end 

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

我知道Proc块的语法,但我很困惑'f'变量来自哪里?

回答

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