Steve
2014-Mar-11 15:37 UTC
Four way JOIN (many-to-many + lookup); preventing duplicates when using scopes
I have two "top-level" entities joined with a third table (many-to-many relationship) which uses yet another table (lookup/reference table) to describe the nature of the relationship of the join. ActiveRecord is adding an unnecessary JOIN which results in duplicate rows being returned. [See model class definitions below.] # users INNER JOIN request_user_roles INNER JOIN request_role_types @request.users.count # 1 # users INNER JOIN request_user_roles INNER JOIN request_role_types INNER JOIN request_user_roles @request.users.is_request_initiator.count # 898 @request.users.is_request_initiator.uniq.count # 1 # users INNER JOIN request_user_roles INNER JOIN request_role_types INNER JOIN request_user_roles @request.users.joins(:request_role_types).merge(RequestRoleType.is_initiator).count # 898 I want to select users for a particular request based on their role with respect to it. I can write this in SQL, and I can write this in a single AR query, but I would like to simplify my code with scopes, if possible. What I can't do is structure a scope such that I don't return duplicates. Or, to put it another way, I can't structure the scope to not automatically include the extra JOIN (on request_user_roles). Is there a way to express this with AR scopes or in another modular, re-usable way? The uniq() feels dirty. # relevant classes # User # | # RequestUserRole - RequestRoleType # | # Request class User < ActiveRecord::Base has_many :request_user_roles has_many :requests, :through => :request_user_roles has_many :request_role_types, :through => :request_user_roles scope :is_request_initiator, joins(:request_role_types).where(:request_role_types => {:request_role_type => 'Initiator'}) end class Request < ActiveRecord::Base has_many :request_user_roles has_many :users, :through => :request_user_roles has_many :request_role_types, :through => :request_user_roles end class RequestUserRole < ActiveRecord::Base belongs_to :user belongs_to :request belongs_to :request_role_type end class RequestRoleType < ActiveRecord::Base has_many :request_user_roles scope :is_initiator, where(:request_role_type => 'Initiator') end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f443dfaf-1394-454a-bd64-9b2a404965f0%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.