1

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

class Following < ActiveRecord::Base

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

 end

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

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

4

1 回答 1

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
于 2013-05-26T09:52:35.547 回答