title: Update attributes on has_many through associations and working with the unsaved object
body: <p>This has something to do with my <a href="https://stackoverflow.com/questions/26651811/rails-activerecord-has-many-through-association-on-unsaved-objects">last quesion about unsaved objects</a>, but now it is more about a specific problem how to use rails.</p>
  
  <p>The models I have are:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
      has_many :project_participations
      has_many :projects, through: :project_participations, inverse_of: :users
  end
  
  class ProjectParticipation &lt; ActiveRecord::Base
      belongs_to :user
      belongs_to :project
  
      enum role: { member: 0, manager: 1 }
  end
  
  class Project &lt; ActiveRecord::Base
      has_many :project_participations
      has_many :users, through: :project_participations, inverse_of: :projects
  
      accepts_nested_attributes_for :project_participations
  end
  </code></pre>
  
  <p>With this models, when I create a new project I can do it by a form (<code>fields_for</code> etc) and then I can call <code>update_attributes</code> in the controller. So if I have users in the database already, I can do this:</p>
  
  <pre><code>u = Users.create # save one user in database (so we have at least one saved user)
  p = Project.new
  
  # add the user to the project as a manager
  # the attributes could come from a form with `.fields_for :project_participations`
  p.update_attributes(project_participations_attributes: [{user_id: u.id, role: 1}])
  =&gt; true
  </code></pre>
  
  <p>This works fine until I want to do something with the <code>users</code> of a project. For example I want add a validations that there must be at least one <code>user</code> for a project:</p>
  
  <pre><code>class Project &lt; ActiveRecord::Base
      ...
      validates :users, presence: true # there must be at least one user in a project
      ...
  end
  </code></pre>
  
  <p>This now gives:</p>
  
  <pre><code>u = Users.create
  p = Project.new
  
  p.update_attributes(project_participations_attributes: [{user_id: u.id, role: 1}])
  =&gt; false
  
  p.errors
  =&gt; #&lt;ActiveModel::Errors:... @base=#&lt;Project id: nil&gt;, @messages={:users=&gt;["can't be blank"]}&gt;
  
  p.users
  =&gt; #&lt;ActiveRecord::Associations::CollectionProxy []&gt;
  
  p.project_participations
  =&gt; #&lt;ActiveRecord::Associations::CollectionProxy [#&lt;ProjectParticipation id: nil, user_id: 1, project_id: nil&gt;]&gt;
  </code></pre>
  
  <p>So on unsaved <code>projects</code> the <code>.users</code> is empty. This already bugs me (see <a href="https://stackoverflow.com/questions/26651811/rails-activerecord-has-many-through-association-on-unsaved-objects">my last quesion about unsaved objects</a>). But in this case I can of course now work around this by doing <code>validates :project_participations, presence: true</code> instead of <code>validates :users, presence: true</code> and it should mean the same.</p>
  
  <p>But this would mean I should never use the <code>.users</code> method (in any helper, model, view, ...) unless I am totally sure that I work with a saved object. Which in fact renders the <code>.users</code> method unusable (like it does with the validation of user`s presence).</p>
  
  <p>If I call <code>update_attributes</code> like this, the validations works and it saves:</p>
  
  <pre><code>p.update_attributes(users: [u])
  </code></pre>
  
  <p>With this it creates the <code>project_participation</code> by itself so <code>p.users</code> works as expected. But here I cannot set any data like <code>role</code> for <code>project_participation</code> of that user.</p>
  
  <p>So my questions are: Can I make the <code>.users</code> method work whether or not the object is saved (I think not)? But then, how can I add users to a unsaved project as a manager/member and work with the unsaved project?</p>
  
  <p>I hope my problem is clear.</p>