Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using multiple scope params in Rails3

In my Rails3 app I have AR scope which requires 3 parameters

Ex: I'm trying to get an error details for a given module between two code values

#select * from error_codes where error_module_id=1 and code >0 and code < 100

scope :latest_error_code, lambda{ |module_id, min, max|
    {:conditions => "error_module_id=#{module_id} and code >= #{min} and code <= #{max}"}
} 

in my console I do

ErrorCode.latest_error_code(1,0,100)

But when I try to execute this, I'm getting the following error

multiple values for a block parameter (3 for 1)

and when i did some goggling, it appears to be that AR scope doent support multiple parameters

1 - is it true? (AR doent support multiple params for scope) 2 - Is there any other alternative? 3 - Am I doing something wrong here?

thanks in advance

like image 764
sameera207 Avatar asked Jan 26 '12 06:01

sameera207


1 Answers

From the Active Record Query Interface Guide:

Using a class method is the preferred way to accept arguments for scopes.

So you probably want something more like this:

def self.latest_error_code(module_id, min, max)
    where(
        'error_module_id = :module_id and code between :min and :max',
        :module_id => module_id, :min => min, :max => max
    )
} 
like image 100
mu is too short Avatar answered Nov 01 '22 23:11

mu is too short