rohith
rohith

Reputation: 106

querying to check for integer values in ruby on rails

I want to execute this query in rails app

   Video.where("category= 'film' AND grant = 1").order('created_at DESC').page(params[:page]).per_page(10)

here grant stores an integer value.

and I am getting following error

    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 'grant = 1) ORDER BY created_at DESC LIMIT 10 OFFSET 0' at line 1: SELECT  `videos`.* FROM `videos`  WHERE (category= 'film' AND grant = 1) ORDER BY created_at DESC LIMIT 10 OFFSET 0

thanks in advance

Upvotes: 3

Views: 307

Answers (3)

SR5
SR5

Reputation: 95

Check this:

Video.where("category = ? AND `grant` = ?", "film", 1).order('created_at DESC').page(params[:page]).per_page(10)

Upvotes: 0

nbbk
nbbk

Reputation: 1102

The problem is with grant. GRANT is a reserved word in MySql. You should use back-ticks around them.

Video.where("category= 'film' AND `grant` = 1").order('created_at DESC').page(params[:page]).per_page(10)

will do the job i guess. Refer Reserved Words

Upvotes: 5

user2076561
user2076561

Reputation: 159

Try this ..

@videos=Video.where(:category => 'film',:grant => 'yes' ).order('created_at DESC').page(params[:page]).per_page(10)

Upvotes: 4

Related Questions