简体   繁体   中英

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

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
    )
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM