InesM InesM - 1 year ago 55
Ruby Question

ruby on rails - Use method as query column

I what to use a method as a query column on rails.

My model looks like this:

class CourseDate < ActiveRecord::Base
belongs_to :course, touch: true

validates :date, presence: true
validates :start, presence: true
validates :end, presence: true

def start_datetime
DateTime.new(date.year, date.month, date.day, start.hour, start.min, start.sec, start.zone)
end

end


And what I want to do is something like this:

CourseDate.where("start_datetime > ?", Time.now)


This query is returning:
column "start_datetime" does not exist


Is there a way to achieve this?

Answer Source

PostgreSQL supports date + time (doc), so you could write the query like this :

CourseDate.where('"date" + "start" > ?', Time.now)