Reputation: 331
I have this search method that works well on my local computer using Sqlite3, but doesn't work on Heroku which uses pg. What better way can I write this method to work well on the two.
def self.search(origin, destination, departure_date)
departure_date = "#{departure_date}%"
Flight.where("origin_id = ? AND destination_id = ? AND departure_date LIKE ?", origin, destination, departure_date)
end
Upvotes: 0
Views: 51
Reputation: 4164
I'm guessing you are doing this because of the fact that you do not want to include the timestamp on your query? because you want to match only the date on the departure_date
column?
If that is the case, a sure way to do this is to use the sql date
function to get the date part of you date + timestamp
.
This way, you can do something like the following:
def self.search(origin, destination, departure_date)
Flight.where("origin_id = ? AND destination_id = ? AND date(departure_date) = ?", origin, destination, departure_date)
end
(Assuming departure date
is a valid date in string format).
This is as seen from the SQL documentation on the date
function.
The DATE() function extracts the date part of a date or date/time expression.
Fortunately, this works the same way for both SQL
and postgresql
.
A word of Advice, however: It is a good practice to anticipate the type of environment that will be used for hosting and what will/won't be supported so that these conditions could be simulated on the development environment for easy replication of errors and bugs.
What I'm trying to say is that if the hosting environment will be using postgresql
, making use of the same postgresql
on your development environment is a good thing.
Upvotes: 1