Hi have a model like this:
class EventDate < ActiveRecord::Base
belongs_to :event
named_scope :named, lambda { | name | {
:joins => { :event => :core},
:conditions => ["name like ?", "%#{ name }%"]
}}
named_scope :date_range, lambda { | start, length | {
:conditions => ["day >= ? AND day <= ?", start, date + (length || 30) ]
}}
it works correctly if i launch
name = “ba”
start = Date.today
EventDate.named(name).date_range(start , start + 2)
But if the name or the start is nil i don’t want execute the named_scope like
name = nil
EventDate.named(name).date_range(start , start + 2)
Is possible to set a condition inner the named_scope ?
thanks
Yes,
just do:
Inside the lambda.
In this way you can also set a default query to
execute if the param is not given