Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can a record belong_to a single user, but also have multiple "secondary" users?
    primarykey
    data
    text
    <p>In my app, I have a User model and a Project model. A user has_many assignments and each project belongs_to a user. But along with each project having an owner, the user who created it, I would like the owner be able to share it with others (so that the project gets shown on the other users' account along with their own). I imagine having to use has_many :through, and setting up a projects_users table with a user_id and a project_id. And I guess this would be the end result?</p> <pre><code>Project.first.user # The creator of the project =&gt; #&lt;User id: 1, name: 'andrew', etc...&gt; Project.first.users # The users that the creator chose to share it with =&gt; [#&lt;User id: 2 ...&gt;, #&lt;User id: 3 ...&gt;] </code></pre> <p>I've been working on this a bit, and I created a SharedProject model with a user_id and project_id column:</p> <pre><code>class SharedProject &lt; ActiveRecord::Base belongs_to :user belongs_to :project end </code></pre> <p>I would like to call both user.projects and user.shared_projects, but I don't know how I would get shared_projects to return project records instead of records from the shared_projects table. I can't do <code>has_many :projects, :through =&gt; :shared_projects</code> since then I wouldn't be able to return projects that the user has created.</p> <pre><code>class User &lt; ActiveRecord::Base has_many :projects # Calling user.projects returns the projects that user created has_many :shared_projects # How to get user.shared_projects to return project records? end </code></pre>
    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