Thanks RipTheJacker. That's definitely the bug I was getting at, and that workaround helps!
On Apr 28, 3:52 pm, RipTheJacker <[email protected]> wrote: > This is the query that is done when you search on an empty array: > > SELECT "id" FROM "users" WHERE "id" IN (SELECT "user_id" FROM > "memberships") ORDER BY "id" > > That is incorrect, and a bug. You can get around this by using two > statements: > > memberships = Membership.all(:project_ids => []) # => will return an > empty array, as expected > memberships.users # => will return [] or the users > > It probably isn't ideal, but at least when you do > Membership.all(:project_ids => []) on an empty array, no query gets > run. > > On Apr 20, 1:53 pm, Michael Kebbekus <[email protected]> wrote: > > > > > > > > > Hey sonlic, > > > That looks about right as per our setup, but what happens when you do > > Membership.all(:project_id => []).user with an empty array? > > > I'm suggesting that I've found a bug, because I get back the whole > > user table even though Memberhip.all(:project_id => []) correctly > > returns an empty Datamapper::Collection. > > > >> Membership.all(:project_id => []) > > => [] > > >> Membership.all(:project_id => []).class > > > => DataMapper::Collection > > > >> Membership.all(:project_id => []).user #returns every User!?!? > > > On Apr 20, 12:10 am, Piotr Solnica <[email protected]> wrote: > > > > Hi Michael > > > > On Apr 20, 1:11 am, Michael Kebbekus <[email protected]> wrote: > > > > > Isn't this supposed to be a more efficient way of saying > > > > Membership.all(:project_id => []).map(&:user) which returns []? > > > > Yes it is. I don't know how your relationships are set up exactly, but > > > the following example works for me: > > > > class Project > > > include DataMapper::Resource > > > > property :id, Serial > > > > has n, :memberships > > > has n, :users, :through => :memberships > > > end > > > > class Membership > > > include DataMapper::Resource > > > > belongs_to :project, :key => true > > > belongs_to :user, :key => true > > > end > > > > class User > > > include DataMapper::Resource > > > > property :id, Serial > > > > has n, :memberships > > > has n, :projects, :through => :memberships, :via => :project > > > end > > > > DataMapper.finalize > > > DataMapper.auto_migrate! > > > > project = Project.create > > > user = User.create > > > > project.users << user > > > project.save > > > > # you can grab users either via project: > > > Project.all(:id => [project.id]).users > > > > # or membership > > > Membership.all(:project_id => [project.id]).user > > > > Hope this helps > > > > Cheers! > > > > # solnic -- You received this message because you are subscribed to the Google Groups "DataMapper" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.
