InesM InesM - 2 years ago 68
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, date.month,, start.hour, start.min, start.sec,


And what I want to do is something like this:

CourseDate.where("start_datetime > ?",

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" > ?',
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download