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
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With