mysql - Rails 3.2 - ActiveRecord `where` query with method called on attribute -
in console can query job table this: jobs = job.where("created_at <= ?", time.now)
i want jobs created year. if individual job can utilize job.created_at.year if seek utilize jobs = job.where("created_at.year = ?", 2014) next error:
sqlite3::sqlexception: no such column: created_at.year i think understand problem i'm trying mix ruby sql query i'm not sure how around it. understanding help me create much more powerful activerecord queries.
edit found can using .map so: job.all.map { |j| j if j.created_at.year == 2014 }. best way if want gather collection of jobs requires calling method on attribute?
try this:
job.where(:created_at => time.now.beginning_of_year..time.now.end_of_year) since, calling time.now potentially give 2 different years (before , after midnight on dec 31) improve first create variable current time before used in query:
current_time = time.now job.where(:created_at => current_time.beginning_of_year..current_time.end_of_year) mysql ruby-on-rails ruby database activerecord
No comments:
Post a Comment