lundi 4 janvier 2016

Ruby on Rails: Query in model for SQLite in Dev => PostgreSQL in Prod

I am currently running SQLite in my development environment and PostgreSQL in the production environment on Heroku.

My current search functionality has the following method in the model entry.rb:

def self.search(search)
  where("content LIKE ? OR created_at LIKE ?", "%#{search}%", "%#{search}%") 
end

While this might work in my development environment running SQLite – Heroku running PostgreSQL is throwing up an error.

I am aware of the fact that it is bad practice to run different databases in different environments, however, for now I'll stick to it for ease of administration.

Having said that, I am wondering what the correct query would look like for PostgreSQL?

Also, could I add an if clause in my model that feeds one query or the other based on the environment I am in? Like:

if Rails.env.development?
  # SQLite query
else
  # PostgreSQL query
end

Thanks guys!

Aucun commentaire:

Enregistrer un commentaire