Note that there are some explanatory texts on larger screens.

plurals
  1. POConnecting two parallel join models in Rails
    primarykey
    data
    text
    <p>Let's say that I have Projects, Users, and two ways to connect Users to Projects - Rsvps and Memberships. Like so:</p> <pre><code>class User &lt; ActiveRecord::Base has_many :memberships has_many :rsvps has_many :projects, :through =&gt; :memberships end class Project &lt; ActiveRecord::Base has_many :memberships has_many :rsvps has_many :members, :through =&gt; :memberships, :class_name =&gt; 'User' end class Rsvp &lt; ActiveRecord::Base belongs_to :project belongs_to :user end class Membership &lt; ActiveRecord::Base belongs_to :project belongs_to :user end </code></pre> <p>Rsvps and Memberships are different enough in their other functions that I don't especially want to combine them into one model, but I do want to be able to associate Rsvps and Memberships. My ideal-world scenario would be something like this:</p> <pre><code>class Membership &lt; ActiveRecord::Base has_one :rsvp, :through =&gt; [:project, :user] end </code></pre> <p>but obviously that's a pipe dream.</p> <p>The two potential solutions I've thought of so far are trying to fake an association with something like</p> <pre><code>def rsvp project.rsvps.find(:conditions =&gt; {:user_id =&gt; user_id}) end </code></pre> <p>but that's less than ideal as it doesn't duplicate all the fun other features that come with Association objects. Alternatively I could add a <code>membership_id</code> field to Rsvp, but that smells like it'll attract syncing-related bugs.</p> <p>Does anyone have any better ideas?</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