mercredi 28 janvier 2015

Rails 4 sql query interpretation with mysql or sqlite3

I'm using sqlite in development and mysql in production.


One the same SQL query works fine in the development mode and gives an error when executing in the production.


In sqlite (works!):



SELECT "events".* FROM "events" WHERE ("events"."id" = ? OR "events"."deadline" BETWEEN '2015-01-28 22:56:05.651383' AND '2016-01-28 22:56:05.651385'


In mysql (doesn't work):



SELECT `events`.* FROM `events` WHERE (`events`.`id` = OR `events`.`deadline` BETWEEN '2015-01-28 22:24:10.837686' AND '2016-01-28 22:24:10.837690')


The error message:



ERROR -- : Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'OR `events`.`deadline` BETWEEN '2015-01-28 22:24:10.837686' AND '2016-01-28 22:2' at line 1: SELECT `events`.* FROM `events` WHERE (`events`.`id` = OR `events`.`deadline` BETWEEN '2015-01-28 22:24:10.837686' AND '2016-01-28 22:24:10.837690')


Controller action code



def edit
query = Event.unscoped.where( deadline: (Time.now)..(Time.now + 1.year), id: @booking.event_id )
@events = Event.where(query.where_values.inject(:or))
end


Mysql misses "?" sign and I don't understand why. Please, help!


Aucun commentaire:

Enregistrer un commentaire