-1

I have a movies table in the database with separate columns (integers) for month and year of release date.

I want to get all the movies with the release date from 09/2016 till 02/2017.

This query won't work because of other unwanted results:

Movie.where(month: [09, 10, 11, 12, 1, 2], year: [2016, 2017])

How do I query making pairs of month & year?

Nadiya
  • 1,421
  • 4
  • 19
  • 34

1 Answers1

1

Storing dates in that way is a very poor design decision (if you don't also store an actual date value).

So, either do data migration and create a date column which will be trivial to query, or do a big-ass conditional query:

SELECT movies.*
FROM movies
WHERE month = 9 AND year = 2016 OR
  month = 10 AND year = 2016 OR
  ...
  month = 2 AND year = 2017

To build this query in activerecord, you can use this answer (rails-5 specific).

Community
  • 1
  • 1
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367