0

I have this function in one of my Rails models:

def delete_unactivated_users    
  expiry_time = Time.zone.now - USER_ACTIVATION_TIME
  User.where("admin == ? and activated == ? and created_at < ?", false, false, expiry_time).destroy_all
end

In development mode, with SQLite, it works perfectly well. But in production, with MySQL, I get this syntax error:

ActiveRecord::StatementInvalid (Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '== 0 and activated == 0 and created_at < '2014-05-20 10:35:55')' at line 1: SELECT `users`.* FROM `users`  WHERE (admin == 0 and activated == 0 and created_at < '2014-05-20 10:35:55'))

How can my function be fixed so that it works equally well in SQLite and MySQL?

This would be an excerpt of my schema.rb file:

create_table "users", force: true do |t|
  ...
  t.boolean  "admin",                         default: false
  t.boolean  "activated",                     default: false
  ...
end

Thanks for any help.

1
  • 1
    This is what you get when you use different databases for development and production. Expect more surprises to follow. Commented May 30, 2014 at 10:59

1 Answer 1

3

MySql doesn't use the == - it uses = to test equality as well as set a value.

Change it to = as that is the SQL standard.

Ie

  User.where("admin = ? and activated = ? and created_at < ?", false, false, expiry_time).destroy_all
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.