Home rails difference between two dates inside .where
Reply: 0

rails difference between two dates inside .where

user1112 Published in March 19, 2018, 12:57 am

Here is my logic I want to get the closest 4 more expensive mobiles to a specific mobile @mobile but under one condition the difference between the release dates of the two mobiles is not more than a year and half Here is the query

high = Mobile.where("price >= #{@mobile.price} AND id != #{@mobile.id} AND visible = true").where("ABS(release_date - #{@mobile.release_date}) > ?", 18.months).order(price: :ASC).first(4)

The first .where() works perfectly but the second is not working and I get this 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 '00:00:00 UTC) > 46656000) ORDER BY `mobiles`.`price` ASC LIMIT 4' at line 1: SELECT `mobiles`.* FROM `mobiles` WHERE (price >= 123123.0 AND id != 11 AND visible = true) AND (ABS(release_date - 2016-04-10 00:00:00 UTC) > 46656000) ORDER BY `mobiles`.`price` ASC LIMIT 4

I think now you can get my logic. What is the right syntax to achieve it?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.295764 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO