ruby on rails - Search model with time range substitution -
one can search model time range this:
user.where(updated_at: previous_update..now)
however need more complex query:
user.where("updated_at = :updated_at , status != :status", updated_at: previous_update..now, status: "employee")
and turns out that
user.where("updated_at = :updated_at", updated_at: previous_update..now)
doesn't work. looks previous_update..now
should somehow translated sql format before can substituted this. or maybe there's better way express complex query above?
edit
i used jon's suggestion chain where
filters:
now = time.now updates = user.where(updated_at: previous_update..now) employee_updates = updates.where(status: "employee") non_employee_updates = updates.where('status != :status', status: "employee")
you can either chain where
queries:
user.where(status: 'employee').where(updated_at: previous_update..now)
or can combine them within same where
clause:
user.where(status: 'employee', updated_at: previous_update..now)
with placeholder conditions, need wrap them in curly braces:
user.where('updated_at = :updated_at , status != :status', {updated_at: previous_update, status: "employee"})
although can't check equality against date range, checking updated_at
equal previous_update..now
fail. you'd need generate various conditions being greater or equal start date , less or equal end date.
based on ... i'd stick non-placeholder format simple query this.
Comments
Post a Comment