17

I am 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 is 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?

1 Answer 1

30

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.

2
  • Thank you very much for helping. but got error 'ArgumentError: wrong number of arguments (0 for 1)' even i used named_scope Apr 15, 2013 at 17:46
  • The addresses argument needs to be supplied in noaddresses(addresses_arguement) or removed. Apr 15, 2013 at 18:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.