i have userevent class event management app, , user event can have many event dates when events occur:
class userevent < activerecord::base ... has_many :event_dates ... end
the eventdate class has user_event_date property:
class eventdate < activerecord::base attr_accessible :user_event_date, :start_time, :end_time belongs_to :user_event, inverse_of: :event_dates ... end
i have search class, , when search executed, find_user_events method filters user events, user events remain user_event_date between search object user_event_date_from , user_event_date_to properties.
class search < activerecord::base attr_accessible :user_event_date_from, :user_event_date_to private def find_user_events if user_event_date_from.present? || user_event_date_to.present? time_range = user_event_date_from..user_event_date_to user_events = userevent.joins(:event_dates).where(event_dates: {user_event_date: time_range}) end end
but filter not work, records returned. got "where" code rails guide under section "specifying conditions on joined tables":
so questions:
- how work?
- if user specifies user_event_date_from (user_event_date_to nil) work, range user_event_date_from nil, or should detect , set range to: user_event_date_from..user_event_date_from
thanks in advance!
shouldn't tricky debug .. open rails console , stick in
userevent.joins(:event_dates).where(event_dates: { user_event_date: 10.days.ago..datetime.now })
..or change dates know data returned. check data correct. if works problem elsewhere.
as other question, if try , use nil
in range you'll argumenterror: bad value range
. yes, depending on how want behave, usual solution check nil values , replace appropriate behaviour you'd implement.
edit - oddly, nil..nil
range doesn't produce argumenterror, mean you're not setting values successfully. how setting values of user_event_date_from
, user_event_date_to
?
Comments
Post a Comment