0

I have an array of products ids, which I would like to use for filtering a list of buying sessions. So far so good:

Session.where('product_id': [23, 32, 17])

I also have an array of dates, which specify for each product (differently) from which date should I even consider their sessions: ['12/2/17', '1/1/17', '20/3/17']

So in other words: product id 23, should only be considered for sessions created after 12/2/17, product id 32, should only be considered for sessions created after 1/1/17, and so on...

What is the right rails command to also consider the product dates when selecting sessions about the wanted product ids?

1 Answer 1

2

Something like this, chaining your where clauses to your needs.

Session.where('product_id = ? AND date > ?', 23, Time.new(2017, 2, 12))
  .or(Session.where('product_id = ? AND date > ?', 32, Time.new(2017, 1, 17)))
Sign up to request clarification or add additional context in comments.

2 Comments

Thats what I have for now as the 'naive' solution (I think). will it be maybe faster to add a column of the date dynamically (in a kind of a 'join' operations) and then use the column data in another where clause?
@dneumark You can probably generate this programmatically. For example (apologies for the poor formatting within a comment): create a 2D array: where_clause = [ [23, '12/2/17'], [32, '1/1/17'], [17, '20/3/17'] ]. Then iterate through it where_clause.each do |(product, date)|, and apply the clause: Session.or(Session.where('product_id=? AND date>?', product, date))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.