mardi 3 mars 2015

Skip instance in conditional callback block (lambda or Proc)

I noticed that almost all people use Proc.new instead of lambda (I guess because of how it cares/or not about arity) and also it's common to pass the current instance as an argument. However I checked and it works also without |instance| passed as an arg. See the example below



class SomeModel < ActiveRecord::Base
has_many :associated_objects

before_save :do_something, if: -> { associated_objects.empty? }
end


According to official guide and most posts found on internet it seems that I should write



before_save :do_something, if: Proc.new { |instance| instance.associated_objects.empty? }


Is there something wrong with my example using -> and no argument?


Aucun commentaire:

Enregistrer un commentaire