title: Rails has many and belongs to one
body: <p>I have a <code>User</code> model which has many <code>projects</code> and a <code>Project</code> model which can have many <code>users</code>, but also belongs to a single user (ie the user who created this project). It must belong to a <code>User</code>. It also allows a list of users to be associated with it, think collaboration.</p>
  
  <p>With this in mind, my models look like this:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :assigned_projects
    has_many :projects, :through =&gt; :assigned_projects
  end
  
  class Project &lt; ActiveRecord::Base
    belongs_to :user
    has_many :assigned_projects
    has_many :users, :through =&gt; :assigned_projects
  end
  
  class AssignedProject &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :project
  end
  </code></pre>
  
  <p>Now, when I want to create a new project through a <code>User</code>, this is how I would do it:</p>
  
  <pre><code>user = User.create(:name =&gt; 'injekt')
  user.projects.create(:name =&gt; 'project one')
  </code></pre>
  
  <p>Now, I know that <code>projects</code> is provided through an <code>AssignedProject</code> join model, which is why <code>project.user</code> will return <code>nil</code>. What I'm struggling to get my head around is the best way to assign the project creator (which by the way doesn't <strong>need</strong> to be <code>user</code>, it could be <code>creator</code> or something else descriptive, as long as it is of type <code>User</code>).</p>
  
  <p>The idea then is to create a method to return <code>projects_created</code> from a <code>User</code> which will select only projects created by this user. Where <code>user.projects</code> will of course return ALL projects a user is associated with.</p>
  
  <p>Assuming this kind of association is <em>fairly</em> common, what's the best way to achieve what I want? Any direction is greatly appreciated.</p>