brad
brad

Reputation: 1695

Writing postgresql method on model to get datetime entries by day of week

I have a column in one of my model's (team_opps) that is a datetime column called start_date. I recently migrated my db from sqlite3 to postgresql and this blew up the method.

Here is the method that was working in sqlite3:

  def self.monday_team_opps
    where('strftime("%w", "start_date") = "?"', 1)
  end

The error that it gives is:

PG::UndefinedColumn: ERROR:  column "%w" does not exist

Meaning it doesn't like the strftime. Can anyone clarify what needs to be adjusted to make this work in pgsql?

Thanks!

Upvotes: 0

Views: 419

Answers (2)

nikolayp
nikolayp

Reputation: 17939

def self.monday_team_opps
  where("extract(day from start_date) = ?", 1)
end

Upvotes: 0

usha
usha

Reputation: 29359

Try this

 def self.monday_team_opps
    where('EXTRACT(DOW FROM start_date) = ?', 1)
 end

Upvotes: 4

Related Questions