在 Rails 控制台中,我得到了这些结果:
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'
但是当我直接输入方法时:
Employee.find_by_shop_id(2)
=> #<Employee id: 2, first_name: "Roger", last_name: "Bartoletti", email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="523f33203b3e3b370d3a333c37123a3320203b217c303b28" rel="noreferrer noopener nofollow">[email protected]</a>", shop_id: 2>
为什么#find_by_shop_id无法“发送”?
谢谢
最佳答案
您尝试执行的方法名为 find_by_shop_id
,而不是 find_by_shop_id(2)
。 send
method 需要一个方法名称,后跟参数。例如,
Employee.send "find_by_shop_id", 2
关于ruby-on-rails - rails 3 : can't call an ActiveRecord query with the #send method (NoMethodError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37745961/