ruby on rails - Creating filtering by 'Created_at' and/or presence of data in multiple columns -
big newbie here. started rails recently. i've been searching , i'm stuck on forward progress i've reached filtering.
currently on index page, i'm showing orders created. customers (first_name, last_name) pay 'cash', 'credit', or 'check' (shown in decimal(8,2)). create filter manager can click on date , filter payment type (cash, credit or check), reconciliation purposes.
i have searched many , confused method use. best guess have use sort of 'scoped' methodology? if can point me in right direction, i'd solve myself. in advance!
scopes great in ruby.
http://apidock.com/rails/activerecord/namedscope/classmethods/scope
if want filter date. want supposed happen after today following. put scope definition in model want scope. lets call mimodel
scope :post_dated, where("created_at > current_timestamp")
that dumb example example nonetheless. if want filter after given date can use lambdas
scope :post_dated, lambda { |date| where("date(created_at) > ?" date) }
then call them wherever calling mimodel.post_dated first example , you'll mimodel objects occurred after today. second example call mimodel(date.today) same affect or mimodel((time.now-30.days)) 30 days ago.
your specific problem cannot see cannot scopes define various queries single code-first calls.
Comments
Post a Comment