5

I'm reading this question, where it says, that the calls

something {|i| i.foo }
something(&:foo)

are equivalent.

Now I was trying to refactor my model named AdminUser according to this pattern and replaced

after_create { |admin| admin.send_reset_password_instructions }

with

after_create(&:send_reset_password_instructions)

, but when I'm running my migration, which contains the lines

def migrate(direction)
  super
  # Create a default user
  AdminUser.create!(email: 'a@b.de', password: 'very_clever', password_confirmation: 'very_clever') if direction == :up
end

it gives me the error

ArgumentError: no receiver given

pointing to the line AdminUser.create!....

Can anyone tell me what goes wrong here?

Community
  • 1
  • 1
the-bass
  • 705
  • 1
  • 6
  • 20
  • I see no reason that you cannot replace `after_create { |admin| admin.send_reset_password_instructions }` with the shorter form, so I'd look elsewhere for the problem. You're thinking, "but it worked before I made the change", but I'd double-check that you made no other changes, perhaps inadvertently. – Cary Swoveland May 13 '16 at 18:29
  • 2
    better yet, why not just do `after_create :send_reset_password_instructions` – photoionized May 13 '16 at 18:53

1 Answers1

0

I know this is an older question, but it interested me quite a bit causing me to do some research of my own. I don't have a corrected code answer for you, but in looking around I believe this post what happened when pass a method to iterator method is very closely related and will most likely answer your question of "what's going wrong here?"

Basically because you're now passing it a proc with the refactored code it's expecting a specific argument that your AdminUser is no longer passing it and causing the error of it not having a receiver.

That being said, I'm sure you have your reasons of setting up your code the way you do, but based on the implied idea of what you're doing and the context I would agree with @photoionized with using after_create :send_reset_password_instructions as it's clear, concise and (most likely) has your desired outcome.

Community
  • 1
  • 1
Tim Higgins
  • 383
  • 2
  • 5
  • 15