Getting through ActiveRecord people linking two given person?

You are looking at a fairly complex algorithm Do a search for breadth-first and depth-first search to get ideas how to implement a recursive method in your Person model to do this One general suggestion: set up Person-to-Person associations in your Person model, like this: has_many :from_relations, :source => :from_person, :through => :from_relationships has_many :to_relations, :source => :to_person, :through => :to_relationships Then you can get a collection of relations with @person. From_relations and @person. To_relations Depending on your application needs, you may be able to simplify things further by dealing with direction in your relationship model, like this: Person model: has_many :relationships has_many :relations, :through => :relationships Relationship model belongs_to :person belongs_to :relation, :class_name => "Person With the more simple assocations, an instance method in your Person model to find if two persons are related would look something like this: def related_to?(target) if self == target return true elsif self.relations.

Empty? Return false else return self.relations. Each {|relation| relation.

Related_to?(target)} end end Note it uses recursion. Also, I didn't work through the algorithm to make sure there is no potential for infinite loops due to cyclical associations (Joe -> Bob -> Joe -> Bob ).

You are looking at a fairly complex algorithm. Do a search for breadth-first and depth-first search to get ideas how to implement a recursive method in your Person model to do this. One general suggestion: set up Person-to-Person associations in your Person model, like this: has_many :from_relations, :source => :from_person, :through => :from_relationships has_many :to_relations, :source => :to_person, :through => :to_relationships Then you can get a collection of relations with @person.

From_relations and @person. To_relations. Depending on your application needs, you may be able to simplify things further by dealing with direction in your relationship model, like this: Person model: has_many :relationships has_many :relations, :through => :relationships Relationship model belongs_to :person belongs_to :relation, :class_name => "Person" With the more simple assocations, an instance method in your Person model to find if two persons are related would look something like this: def related_to?(target) if self == target return true elsif self.relations.

Empty? Return false else return self.relations. Each {|relation| relation.

Related_to?(target)} end end Note it uses recursion. Also, I didn't work through the algorithm to make sure there is no potential for infinite loops due to cyclical associations (Joe -> Bob -> Joe -> Bob ).

Given a person p1, I want to implement the instance method p1. People_connecting_to(p2) that returns all the people who indirectly link p1 to the other person p2. I want p1.

People_connecting_to(p2) to return p3, p4. Is it possible to achieve in a single SQL request through ActiveRecord? Thanks Ed, your answer leads me to the following solution.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions