Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: Multiple "has_many through" for the two same models?
    text
    copied!<p>Can't wrap my head around this...</p> <pre><code>class User &lt; ActiveRecord::Base has_many :fantasies, :through =&gt; :fantasizings has_many :fantasizings, :dependent =&gt; :destroy end class Fantasy &lt; ActiveRecord::Base has_many :users, :through =&gt; :fantasizings has_many :fantasizings, :dependent =&gt; :destroy end class Fantasizing &lt; ActiveRecord::Base belongs_to :user belongs_to :fantasy end </code></pre> <p>... which works fine for my primary relationship, in that a <code>User</code> can <strong><em>have</em></strong> many <code>Fantasies</code>, and that a <code>Fantasy</code> can <strong><em>belong</em></strong> to many <code>Users</code>.</p> <p>However, I need to add another relationship for <strong><em>liking</em></strong> (as in, a <code>User</code> "likes" a <code>Fantasy</code> rather than "has" it... think of Facebook and how you can "like" a wall-post, even though it doesn't "belong" to you... in fact, the Facebook example is almost exactly what I'm aiming for).</p> <p>I gathered that I should make another association, but I'm kinda confused as to how I might use it, or if this is even the right approach. I started by adding the following:</p> <pre><code>class Fantasy &lt; ActiveRecord::Base ... has_many :users, :through =&gt; :approvals has_many :approvals, :dependent =&gt; :destroy end class User &lt; ActiveRecord::Base ... has_many :fantasies, :through =&gt; :approvals has_many :approvals, :dependent =&gt; :destroy end class Approval &lt; ActiveRecord::Base belongs_to :user belongs_to :fantasy end </code></pre> <p>... but how do I create the association through <code>Approval</code> rather than through <code>Fantasizing</code>?</p> <p>If someone could set me straight on this, I'd be much obliged!</p>
 

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