sql - Rails 3 ActiveRecord Query Syntax for Date Column Plus Integer Column Less Than Today's Date -
my application keeps track of events in database have date , duration. trying query of events status of "upcoming" have date plus duration less today. far have tried:
events = event.where('status = "upcoming" , date <= (? + duration)', date.parse(time.now.to_s))
where date date column, , duration integer representing number of days event runs for. above query produces empty set, , mysql warning "truncated incorrect double value: '2013-05-09'
database mysql, may change in future.
i found sql query provide correct results, not feel railsy enough me due date_add() call:
select `events`.* `events` (status = "upcoming" , date_add(date, interval 2 day) <= '2013-05-09');
if had to, use date_add() function, not know how rails syntax produce such query. ideas?
this solution not fast one:
event.where(status: 'upcoming').select{|e| (e.date + e.duration.days) < date.today}
if i'd change table structure , model. use before_save
hook update new field end of event (date + duration.days). use activerecord results , fast.
have @ http://xyzpub.com/en/ruby-on-rails/3.2/ar_sonstiges.html#ar_callbacks more information before_save
callback.
Comments
Post a Comment