How to retrieve related records in Ruby on Rails? -
How to retrieve related records in Ruby on Rails? -
i trying find user
's overdue invoices
:
class user < activerecord::base def overdue_invoices invoices.where("overdue = ?", true) end end class invoice < activerecord::base def overdue balance > 0 && date.today > due_date end end
the problem seems overdue
method on invoice
model, not database column.
how can retrieve records anyway? , create sense or improve store true
, false
values in database?
you should create equivalent class method or scope overdue
on invoice
:
class invoice < activerecord::base def self.overdue where('balance > 0').where('? > due_date', date.today) end end
then can phone call overdue
on invoice relation:
class user < activerecord::base def overdue_invoices invoices.overdue end end
note i’m assuming due_date
database column, if it’s not, cannot utilize method—however, may able replace sql calculate due date info in columns.
ruby ruby-on-rails-3 activerecord
Comments
Post a Comment