Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have you tried using Rails built in "includes" query association feature?</p> <p>From <a href="http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations" rel="nofollow">http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations</a></p> <p>It should be something like <code>Person.first.friends.includes(:info)</code></p> <p>Update: Ah, you want the Friends info, and there is no association on the model to get that right now.</p> <p>It looks like you are missing an association to get the <code>Person</code> model for the friend (and not for the user). Something like:</p> <pre><code>class Friend &lt; ActiveRecord::Base belongs_to :person belongs_to :friendly_person, :class_name =&gt; "Person", :foreign_key =&gt; "friend_id" end </code></pre> <p>Then you can use the associations you have set up with the includes similar to before <code>Person.first.friends.includes(:friendly_person =&gt; :info)</code> and that should return the info for the friend.</p> <p>Another update: To access the info column for the <code>friendly_person</code> you can use the association. As in <code>friendly_person.info.description</code>. You can't do that on a collection (obviously) you have to have an instance of person. So something like <code>Person.first.friends.includes(:friendly_person =&gt; :info).first.friendly_person.info.description</code> ... but I would like to point out that this type of code is not considered to be good practice.</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