3
我注意到几乎所有的人都使用Proc.new而不是lambda(我猜是因为它关心/不关心arity),并且通常也会传递当前实例作为论据。不过,我检查了它,也没有| instance |作为参数传递。在网上看到,似乎我应该写在有条件的回调块(lambda或Proc)中跳过实例
before_save :do_something, if: Proc.new { |instance| instance.associated_objects.empty? }
是不是有什么毛病我例如使用上找到下面
class SomeModel < ActiveRecord::Base
has_many :associated_objects
before_save :do_something, if: -> { associated_objects.empty? }
end
据官方指南和大多数职位的例子 - >并没有参数?这与在模型代码中使用self.some_method
并使用隐式接收器跳过接收器类似。
我的回答有帮助吗? – Anthony 2015-03-05 13:03:02
是的,我继续使用我在第一篇文章中展示的简短版本 – januszm 2015-03-09 14:52:34