scopes with lambda and arguments in Rails 4 style?

Ruby on-Rails-4ScopeRails Activerecord

Ruby on-Rails-4 Problem Overview


I'm wondering how the following is done in Rails 4 or if I just use the Rails 3 approach for using a lambda that can pass an argument the same way with 4 as I do with 3.

I'm pretty new to Rails 3 and trying to work through some samples running Rails 4.

Here is my Rails 3 code:

class Person < ActiveRecord::Base
  scope :find_lazy, lambda {|id| where(:id => id)}
end

# In console I can call
Person.find_lazy(1)

So if this is the Rails 4 way is to use the -> {}, that's a lambda, right? scope :all_lazy, -> { select("*") } What if I needed an argument. I tried a few different ideas and get argument errors in the console when using the -> {}.

Ruby on-Rails-4 Solutions


Solution 1 - Ruby on-Rails-4

I think it should be:

scope :find_lazy, -> (id) { where(id: id) }

Solution 2 - Ruby on-Rails-4

Ruby has not deprecated the old style of lambda either, so if you feel more comfortable using that by all means go for it.

I don't personally like the stabby lambda's syntax myself but eventually they will probably become the norm so it doesn't hurt to get used to them.

Solution 3 - Ruby on-Rails-4

Rails 4, you can do:

scope :find_lazy, -> (id) { where(id: id) }

That was in the old ruby:

:id => id

Better hash:

id: id

Solution 4 - Ruby on-Rails-4

guy I was usually using the below programming syntax

scope :find_lazy, -> (id) { where(id: id) }

But when I was reviewing my code using Codacy I found it alerting me about this syntax

Use the `lambda` method for multiline lambdas.

I changed it to be and it working well

  scope :find_lazy, lambda {|id|
    where(id: id)
  }

Solution 5 - Ruby on-Rails-4

To support associations:

scope :find_lazy, -> (object) { where(object_id: object.id) }

Solution 6 - Ruby on-Rails-4

scope :find_lazy, -> (id) { where(id: id) }

is equivalent with

self.find_lazy(id)
  where(id: id)
end

Based on ruby on rails guide: Using a class method is the preferred way to accept arguments for scopes.

There is no reason to use scopes together with lambdas in preference to class methods. It is a matter of personal preference. But, if you want to follow the guidelines, you should use the class method when arguments are involved.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkaplanView Question on Stackoverflow
Solution 1 - Ruby on-Rails-4lis2View Answer on Stackoverflow
Solution 2 - Ruby on-Rails-4Branden SilvaView Answer on Stackoverflow
Solution 3 - Ruby on-Rails-4sesperantoView Answer on Stackoverflow
Solution 4 - Ruby on-Rails-4AstmView Answer on Stackoverflow
Solution 5 - Ruby on-Rails-4tokhiView Answer on Stackoverflow
Solution 6 - Ruby on-Rails-4mmsilviuView Answer on Stackoverflow