5
scope :for_user, (lambda {|user_id| a = Follow.follows(user_id); Question.where{user_id.in(a.select{followed_id})}})

Gives me:

`lambda': tried to create Proc object without a block (ArgumentError)

I've read several questions without being able to fix the problem. I am relatively new to Ruby, and just getting started with Rails. I'm probably a bit over my head.

  • 1
    {} are only used for blocks or hashes... you've got them used for params for methods. – Taryn East Mar 21 '13 at 22:59
  • https://github.com/ernie/squeel Shows what I'm aiming for, they use curly braces. – Max Smashdidypwned Lay Mar 21 '13 at 23:02
  • 2
    what they're using is a "hash as a parameter" - but you'll notice that they don't chain extra stuff on the end. for that you'll generally need (). eg: Thing.do_something {:a => :hash} vs Thing.do_something({:a => :hash}).do_something_else({:another => :hash}) – Taryn East Mar 21 '13 at 23:05
  • reading your link more clearly - yes, I can see what they say you can do. Can I just say - ick - I really don't like that syntax! I suspect your issue isn't in the extra {}s then, – Taryn East Mar 21 '13 at 23:13

3 Answers3

3

I don't think you need the () around the lambda, though. How about you try building up from simple fist eg try:

scope :for_user, lambda {|user_id| Question.where(:user_id => user_id) }

just to see if it breaks/works... then add your actually-required functionality piece by piece until something breaks (or it all works)

Taryn East
  • 27,486
  • 9
  • 86
  • 108
1

Just in case if someone is trying to create a multiline lambda with the help of do..end block instead {..} it'll throw the same error.

Not Recommended (will throw an error)

scope :for_user, lambda do |user_id|
  Question.where(user_id: user_id)
          .where(active: true)
  end

Not Recommended (won't throw any error); instead use class methods

scope :for_user, lambda { |user_id|
  Question.where(user_id: user_id)
          .where(active: true)
  }
Swaps
  • 1,450
  • 24
  • 31
0

I was getting the same error and what worked for me was what Taryn East suggested: no ().

scope :event, lambda {|name| active.where(:key => name)}

That's how I got mine to work.

C. Louis S.
  • 354
  • 3
  • 8