Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get info from a third table when joining two tables?
    primarykey
    data
    text
    <p>I have this schema:</p> <p><img src="https://i.stack.imgur.com/oKe9r.png" alt="enter image description here"></p> <ul> <li>There is a table for persons.</li> <li>Each person has one "info" table.</li> <li>Persons can have friends. This is stored in another table called "friends", which has a foreign key for the person, and another one for his friend (both are pointing to the "person" table).</li> </ul> <p><strong>"person" model:</strong></p> <pre><code>class Person &lt; ActiveRecord::Base has_one :info has_many :friends end </code></pre> <p><strong>"info" model:</strong></p> <pre><code>class Info &lt; ActiveRecord::Base belongs_to :person attr_accessible :description end </code></pre> <p><strong>"friends" model:</strong></p> <pre><code>class Friend &lt; ActiveRecord::Base belongs_to :person attr_accessible :friend_id end </code></pre> <p>Let's suppose we have 2 persons: person1 and person2. Each one with a record in the "info" table storing its description. And let's suppose that person2 is a friend of person1.</p> <p><strong>content of "person" table:</strong></p> <blockquote> <p>id: 1</p> <p>id: 2</p> </blockquote> <p><strong>content of "info" table:</strong></p> <blockquote> <p>id: 1, person_id: 1, description: "This is person 1, our main dude."</p> <p>id: 2, person_id: 2, description: "This is person 2, our secondary dude."</p> </blockquote> <p><strong>content of "friends" table:</strong></p> <blockquote> <p>id: 1, person_id: 1, friend_id: 2</p> </blockquote> <p>If I want to get person1's friends I do this:</p> <pre><code>Person.first.friends </code></pre> <p>This will give me:</p> <blockquote> <p>id: 1, person_id: 1, friend_id: 2</p> </blockquote> <p>Now, how do I add the "info" content for each row?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload