I modified the gist a little bit to avoid duplicate Roles, and I seem to get an incorrect result:
############################################## #!/usr/bin/env ruby -Ku # encoding: utf-8 require 'pp' require 'rubygems' require 'dm-core' DataMapper::Logger.new($stdout, :debug) DataMapper.setup(:default, 'sqlite3::memory:') class User include DataMapper::Resource property :id, Serial property :name, String, :length => 1..100, :required => true has n, :roles, :through => Resource end class Role include DataMapper::Resource property :id, Serial property :name, String, :length => 1..20, :required => true has n, :users, :through => Resource end DataMapper.auto_migrate! admin_role = Role.create(:name => 'Administrator') user_role = Role.create(:name => 'User') User.create(:name => 'Dan Kubb', :roles => [ admin_role ]) User.create(:name => 'John Doe', :roles => [ user_role ]) User.create(:name => 'Jane Doe', :roles => [ user_role ]) puts '-' * 80 pp User.all(:roles => { :name => 'Administrator' }) pp User.all(:roles => { :name => 'User' }) ############################################## ...and the output is: ~ (0.000129) SELECT sqlite_version(*) ~ (0.000222) DROP TABLE IF EXISTS "users" ~ (0.000024) DROP TABLE IF EXISTS "roles" ~ (0.000014) DROP TABLE IF EXISTS "role_users" ~ (0.000022) PRAGMA table_info("users") ~ (0.000348) CREATE TABLE "users" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(100) NOT NULL) ~ (0.000010) PRAGMA table_info("roles") ~ (0.000126) CREATE TABLE "roles" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(20) NOT NULL) ~ (0.000009) PRAGMA table_info("role_users") ~ (0.000172) CREATE TABLE "role_users" ("user_id" INTEGER NOT NULL, "role_id" INTEGER NOT NULL, PRIMARY KEY("user_id", "role_id")) ~ (0.000041) INSERT INTO "roles" ("name") VALUES ('Administrator') ~ (0.000037) INSERT INTO "roles" ("name") VALUES ('User') ~ (0.000056) INSERT INTO "users" ("name") VALUES ('Dan Kubb') ~ (0.000060) SELECT "user_id", "role_id" FROM "role_users" WHERE ("user_id" = 1 AND "role_id" = 1) ORDER BY "user_id", "role_id" LIMIT 1 ~ (0.000049) INSERT INTO "role_users" ("user_id", "role_id") VALUES (1, 1) ~ (0.000056) INSERT INTO "users" ("name") VALUES ('John Doe') ~ (0.000049) SELECT "user_id", "role_id" FROM "role_users" WHERE ("user_id" = 2 AND "role_id" = 2) ORDER BY "user_id", "role_id" LIMIT 1 ~ (0.000047) INSERT INTO "role_users" ("user_id", "role_id") VALUES (2, 2) ~ (0.000070) INSERT INTO "users" ("name") VALUES ('Jane Doe') ~ (0.000052) SELECT "user_id", "role_id" FROM "role_users" WHERE ("user_id" = 3 AND "role_id" = 2) ORDER BY "user_id", "role_id" LIMIT 1 ~ (0.000048) INSERT INTO "role_users" ("user_id", "role_id") VALUES (3, 2) -------------------------------------------------------------------------------- ~ (0.000109) SELECT "id", "name" FROM "users" WHERE "id" IN (SELECT "roles"."id" FROM "roles" INNER JOIN "role_users" ON "roles"."id" = "role_users"."role_id" INNER JOIN "users" ON "role_users"."user_id" = "users"."id" WHERE "roles"."name" = 'Administrator') ORDER BY "id" [#<User @id=1 @name="Dan Kubb">] ~ (0.000084) SELECT "id", "name" FROM "users" WHERE "id" IN (SELECT "roles"."id" FROM "roles" INNER JOIN "role_users" ON "roles"."id" = "role_users"."role_id" INNER JOIN "users" ON "role_users"."user_id" = "users"."id" WHERE "roles"."name" = 'User') ORDER BY "id" [#<User @id=2 @name="John Doe">] The inserts are good, but the final user list should include both "John Doe" and "Jane Doe". It looks to me like the query is wrong - it's matching the users.id with the roles.id, not the roles.user_id ? I think this may be the issue but I'm new to DM so I apologize in advance if I did something stupid. I'm running dm-core-0.10.2 on Mac OS X 10.6. Hope this helps Kevin On Feb 15, 3:38 pm, "Dan Kubb (dkubb)" <dan.k...@gmail.com> wrote: > MarkMT, > > > Unfortunately I'm getting > > the wrong people, consistently for a number of different cases. Is > > there some reason why this should not be expected to work? > > Can you try with edge dm-core? There was a recent fix that should > resolve this problem. To confirm I wrote a simple stand-alone script > that demonstrates the correct behaviour when used with edge dm-core: > > http://gist.github.com/305019 > > Please let me know if this resolves your issue. > > -- > > Dan > (dkubb) -- You received this message because you are subscribed to the Google Groups "DataMapper" group. To post to this group, send email to datamap...@googlegroups.com. To unsubscribe from this group, send email to datamapper+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.