David B. David B. - 5 months ago 15
Ruby Question

Rails 3: can't call an ActiveRecord query with the #send method (NoMethodError)

In the rails console I get those results:

Employee.send("find_by_shop_id(2)")
NoMethodError: undefined method `find_by_shop_id(2)' for <Class:0x000000032f75d0> from /var/lib/gems/1.9.1/gems/activerecord 3.1.4/lib/active_record/base.rb:1088:in `method_missing'


But when I type the method directly:

Employee.find_by_shop_id(2)
=> #<Employee id: 2, first_name: "Roger", last_name: "Bartoletti", email: "marilie_hane@harris.biz", shop_id: 2>


How come the #find_by_shop_id cant be "sent" ?

Thanks

Answer

The method you're trying to execute is called find_by_shop_id not find_by_shop_id(2). The send method expects a method name, followed by the parameters. E.g.,

Employee.send "find_by_shop_id", 2
Comments