Cirdes
Cirdes

Reputation: 47

Rails scope works fine with SQLite and MySQL but doesn't on Heroku (PostgreSQL)

scope :unapproved, lambda {|event| where("event_id = ? AND status IS NOT ?",
event.id, "approved")}

The :unapproved scope works with SQLite and MySQL, but it doesn't work on Heroku where it uses PostgreSQL. I can't figure out how to make it work with PostgreSQL.

Upvotes: 1

Views: 379

Answers (2)

mu is too short
mu is too short

Reputation: 434845

PostgreSQL doesn't use IS or IS NOT for general comparisons, you want <> or !=:

where("event_id = ? AND status <> ?", event.id, "approved")

<> should work in MySQL and SQLite as well except when you want to compare against a NULL, then you have to use IS NULL or IS NOT NULL everywhere.

IS is used for special comparisons such as IS NULL and IS DISTINCT FROM, see the Comparison Operators section of the PostgreSQL manual for further details.

If you're planning on deploying on Heroku, you really should be developing on top of PostgreSQL (8.3 for a shared database, 9.0 for a dedicated one). PostgreSQL is a fair bit stricter than SQLite (and MySQL) and no ORM can fully insulate you from the differences between databases. The behavior of GROUP BY is another common problem when moving from SQLite/MySQL to PostgreSQL so you might want to review all you grouping; also, check how you deal with strings that are too long for your varchar columns, AFAIK SQLite ignores your size limits but PostgreSQL will complain loudly if you try to exceed the column size (and MySQL will truncate your data with a mere warning unless you've put your server in strict mode).

Upvotes: 6

Chap
Chap

Reputation: 3563

scope :unapproved, lambda { |event| 
  where("event_id = ?", event.id).
  where("status != ?",  "approved")
}

Upvotes: 0

Related Questions