I need this SQL query Rails-friendly:
SELECT *,
IF (store.availability_offset IS NULL, DATE_ADD(product.release_date, INTERVAL 0 DAY), DATE_ADD(product.release_date, INTERVAL store.availability_offset DAY))
FROM products JOIN stores ON product.store_id = stores.id
WHERE (DATE_ADD(products.release_date, INTERVAL stores.availability_offset DAY) BETWEEN '2013-06-12' AND '2014-07-12' OR DATE_ADD(products.release_date, INTERVAL 0 DAY) BETWEEN '2013-06-12' AND '2014-07-12');
This is my Rails query up to now:
@products = Product.joins(:store).where("products.release_date >= :start_date AND products.publishing_date <= :end_date)", :start_date => params[:search][:start_date], :end_date => params[:search][:end_date]")
What I want to do:
implement the
IFconditional in order to manage the storesavailability_offsetin case its value is NULL.implement the
DATE_ADD()function to be able to add theavailability_offsetvalue to the daterelease date.
How can I do that without using SQL sintax, just ActiveRecord? Thank you!