Skip to content

Instantly share code, notes, and snippets.

@technicalpickles
Created October 13, 2008 15:41
Show Gist options
  • Save technicalpickles/16551 to your computer and use it in GitHub Desktop.
Save technicalpickles/16551 to your computer and use it in GitHub Desktop.
def mutual_relations(relationship_type_name, other_owner, opts = {})
if current_user == self
[]
else
relationship_type = RelationshipType[relationship_type_name]
Owner.find :all, mutual_relation_query(relationship_type, other_owner, opts))
end
end
def mutual_relation_query(relationship_type, other_owner, opts = {})
find_options = {
:joins => %Q{
join owners_relationships as m1
on (owners.id = m1.owner_id and m1.owner_id <> #{self.id})
join relationships as r1
on (m1.relationship_id = r1.id and r1.relationship_type_id = #{relationship_type.id})
join owners_relationships as m2
on (r1.id = m2.relationship_id and m2.owner_id = #{self.id})
join owners_relationships as m3
on (owners.id = m3.owner_id and m3.owner_id <> #{other_owner.id})
join relationships as r2
on (m3.relationship_id = r2.id and r2.relationship_type_id = #{relationship_type.id})
join owners_relationships as m4
on (r2.id = m4.relationship_id and m4.owner_id = #{other_owner.id})
}
}
find_options[:limit] = opts[:limit] if opts.has_key?(:limit)
find_options
end
-<#<Owner id: 3229, first_name: "Jane23", last_name: "Dough The 23", email: "[email protected]", crypted_password: "425a20c02961a5eda2838c1051d9f9316818fe84", gender: nil, phone: nil, mobile: nil, web_site: nil, im_name: nil, im_service: nil, about: nil, created_at: "2008-10-13 15:35:04", locked: false, favorite_agent_id: nil, role_id: 1, time_zone: "Eastern Time (US & Canada)", state: nil, salt: "b4a7c65f536709bab7745939bb5d960185590ba5", remember_token: nil, remember_token_expires_at: nil>>
+<#<Owner id: 812, first_name: "Jane23", last_name: "Dough The 23", email: "[email protected]", crypted_password: "425a20c02961a5eda2838c1051d9f9316818fe84", gender: nil, phone: nil, mobile: nil, web_site: nil, im_name: nil, im_service: nil, about: nil, created_at: "2008-10-13 15:35:04", locked: false, favorite_agent_id: nil, role_id: 1, time_zone: "Eastern Time (US & Canada)", state: nil, salt: "b4a7c65f536709bab7745939bb5d960185590ba5", remember_token: nil, remember_token_expires_at: nil>>
named_scope :mutual_relationships_between, lambda {|owner, other_owner, relationship_type|
{
:joins => %Q{
join owners_relationships as m1
on (owners.id = m1.owner_id and m1.owner_id <> #{owner.id})
join relationships as r1
on (m1.relationship_id = r1.id and r1.relationship_type_id = #{relationship_type.id})
join owners_relationships as m2
on (r1.id = m2.relationship_id and m2.owner_id = #{owner.id})
join owners_relationships as m3
on (owners.id = m3.owner_id and m3.owner_id <> #{other_owner.id})
join relationships as r2
on (m3.relationship_id = r2.id and r2.relationship_type_id = #{relationship_type.id})
join owners_relationships as m4
on (r2.id = m4.relationship_id and m4.owner_id = #{other_owner.id})
}
}
}
def mutual_relations(relationship_type_name, other_owner, opts = {})
if current_user == self
[]
else
relationship_type = RelationshipType[relationship_type_name]
self.class.mutual_relationships_between(self, other_owner, relationship_type).find(:all, opts)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment