coopdevs/katuma — pull request #8 by enricostano (2013-10-25 21:28:09+00:00)
Title: Added better associations for Customer models

Before this change we had:

2.0.0p195 :029 > customer.member_list
Membership Load (0.3ms) SELECT "memberships".* FROM "memberships" WHERE "memberships"."memberable_id" = 1 AND "memberships"."memberable_type" = 'Customer'
Customer Load (0.2ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = 2 LIMIT 1
Customer Load (0.2ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = 3 LIMIT 1
Customer Load (0.2ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = 4 LIMIT 1
=> [#,
#,
#]

but now...

2.0.0p195 :002 > customer.customers
Customer Load (0.1ms) SELECT "customers".* FROM "customers" INNER JOIN "memberships" ON "customers"."id" = "memberships"."member_id" WHERE "memberships"."memberable_id" = 1 AND "memberships"."memberable_type" = 'Customer' AND "memberships"."member_type" = 'Customer'
=> [#,
#,
#]

Furthermore now we could create new relationships just calling:

customer.customers << new_customer