2013-05-26 36 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