Scope but error message ArgumentError: tried to create Proc object without a block

佐手、 提交于 2019-12-10 02:09:35

问题


Hi guys I'm tring to create a scope which find out all contacts with 0 address. Got error message ArgumentError: tried to create Proc object without a block when running command 'Contact.noaddress' in rails c.

here's my contact model including scope

class Contact < ActiveRecord::Base
  attr_accessible :email, :firstname, :lastname, :mobilephone, :fullname
  has_many :addresses
  validates_presence_of :firstname, :lastname


   scope :noaddressed, lambda do |addresses|
    joins(:addresses).where('addresses.created_at.empty?', true)
   end
end

and here's address model

class Address < ActiveRecord::Base
  attr_accessible :city, :country, :postalcode, :region, :street
  belongs_to :contact
end

Could somebody help me please? Thanks a lot.


回答1:


It sounds like a (precedence issue?) referenced here.

If you change the scope to:

scope :noaddresses, (lambda do
  joins(:addresses).where('addresses.created_at is null')
end)

or

scope :noaddresses, lambda { joins(:addresses).where('addresses.created_at is null') }

Also, I don't see where you're using the block argument |addresses|. Did you forget to use it? Otherwise, you can remove it and its surrounding pipes.

Updated: I removed the |addresses| argument and updated the query to be valid SQL syntax.



来源:https://stackoverflow.com/questions/16016668/scope-but-error-message-argumenterror-tried-to-create-proc-object-without-a-blo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!