On Sunday, May 26, 2019 at 8:10:45 AM UTC-7, xura wrote:
>
> I have this hash:
>
> fields = {
>   name: 'John',
>   subject: 'Hello'
> }
>
>
> And I send this hash by this:
>
> Email.where(fields).where(another_condition: 4).all
>
> It works, however, When I want to add OR I need to explicitly define them:
>
> fields = [
>   {
>    name: 'John',
>    subject: 'Hello'
>   },
>   {
>    name: 'Mark',
>    subject: 'Hi'
>
>   }
> ]
>
>
> What I am trying to do here is merge this array of hashes in single WHERE 
> with OR:
>
>
> SELECT * FROM emails WHERE another_condition = 4 AND (PUT ORs HERE)
>
> which should look like:
>
> SELECT * FROM emails WHERE another_condition = 4 AND ((name = "John" and 
> subject = "Hello") OR (name = "Mark" and subject = "Hi"))
>
> How can I do that with Sequel using array of hashes so array oıf hashes 
> becomes ORs (I will add more where (and) after)
>
>
fields = [
  {
   name: 'John',
   subject: 'Hello'
  },
  {
   name: 'Mark',
   subject: 'Hi'

  }
]
DB[:emails].
  where(another_condition: 4).
  where(Sequel.|(*fields))

SELECT * FROM "emails"
WHERE (("another_condition" = 4)
     AND ((("name" = 'John') AND ("subject" = 'Hello')) 
             OR (("name" = 'Mark') AND ("subject" = 'Hi'))))

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/37b3ffed-c032-4d2f-8bdf-e42056b18b00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to