Note that there are some explanatory texts on larger screens.

plurals
  1. POJoining a table with an unconventional column name
    primarykey
    data
    text
    <p>I have the following model :</p> <pre><code>class SocUser &lt; ActiveRecord::Base attr_accessible :email, :id, :name, :username has_many :friends, :class_name =&gt; 'SocFriend', :foreign_key =&gt; 'user_id', :dependent =&gt; :destroy has_many :ownlist, :class_name =&gt; 'SocOwnlist', :foreign_key =&gt; 'user_id', :dependent =&gt; :destroy end class SocFriend &lt; ActiveRecord::Base attr_accessible :user_id, :friend_id belongs_to :user, :class_name =&gt; 'SocUser' belongs_to :friend, :class_name =&gt; 'SocUser' end class SocOwnlist &lt; ActiveRecord::Base attr_accessible :user_id, :book_id belongs_to :user, :class_name =&gt; 'SocUser' belongs_to :book, :class_name =&gt; 'SocBook' end </code></pre> <p>A user can have many friends. And each friend is also a user. Any user can have many books. How do I add an association so that a user can access all the books of his friends. </p> <p>If I add an association in SocUser like <code>has_many :avail_list, :class_name =&gt; 'SocOwnlist', :through =&gt; :friends, :source =&gt; :friend</code></p> <p>The sql query it generates when we call <code>myuser.avail_list</code> is:</p> <pre><code>SELECT "soc_ownlists".* FROM "soc_ownlists" INNER JOIN "soc_friends" ON "soc_ownlists"."id" = "soc_friends"."friend_id" WHERE "soc_friends"."user_id" = 1 </code></pre> <p>But the ON condition should be "soc_ownlists".<b>"user_id"</b> = "soc_friends"."friend_id", is there a way to specify it?</p> <p>Or is there any other way to specify the association?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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