Sudhanva Narayana
Sudhanva Narayana

Reputation: 95

Ruby on Rails: Convert attribute name inside query

I would like to convert type of created_at inside query

 passenger = Passenger.where("created_at.to_date = ?", Date.yesterday)

The .to_date method is not working. Can it be done? Is there a workaround for this

Upvotes: 0

Views: 56

Answers (2)

Dmitriy Gusev
Dmitriy Gusev

Reputation: 151

The proper solution will be is:

 passengers = Passenger.where("DATE(created_at) = DATE(?)", Date.yesterday)

But if you need take only one, you can use

passenger = Passenger.find_by(created_at: Date.yesterday)

I wrote it because name of variable was not plural

Upvotes: -1

ashvin
ashvin

Reputation: 2040

passenger = Passenger.where("DATE(created_at) = ?", Date.yesterday)

Upvotes: 1

Related Questions