is it possible to do a "where in" query with datamapper?
Asked Answered
A

1

5

I would like to get a list of objects that match a specified ID from datamapper.

I know I could use multiple 'or' conditions, but the list of id's can be in the hundreds.

Is there a datamapper command that is equivalent to the following sql?

select * from table where id in (1,2,3,4,5)
Allowedly answered 15/10, 2012 at 13:48 Comment(0)
V
11

You can! It will look something like this:

users = User.all(:id => [1,2,3])

EDIT: you can see this on the github page for dm-core:

  # If the value of a pair is an Array, we do an IN-clause for you.
  Person.all(:name.like => 'S%', :id => [ 1, 2, 3, 4, 5 ])

  # Does a NOT IN () clause for you.
  Person.all(:name.not => [ 'bob', 'rick', 'steve' ])
Visigoth answered 15/10, 2012 at 14:3 Comment(1)
DM monkeypatches Symbol? egads.J

© 2022 - 2024 — McMap. All rights reserved.