mardi 23 février 2016

Rails - Not equal in Active Record

I want to execute a sql query against a table. I'm experiancing a problem in which I cannot proceed.

How can I use Active Record to query a table where column is not equal to null? Current query is given below but it's not working as it gives me a null.

no_sweets = CookieJar.where(cookie_id: array_of_cookies, artificial_sweetner: !nil).count

I want to get count of sweets whose id's are included in array_of_cookies and artificial_sweetner is not null.

Below code works just fine but I need it in Rails way or say I'll need objects to manipulate.

no_sweets = CookieReportingDb.connection.execute("SELECT count(*) FROM cookie_db_new.cookie_jars where artificial_sweetner is not null and cookie_id IN (#{array_of_cookies.join(', ')})").first

I'm using ruby 1.9 and Rails 3.2.

Aucun commentaire:

Enregistrer un commentaire