title: Rails has many through association setting multiple attributes
body: <p>So I have a <code>has_many through</code> association where between two tables <code>posts</code> and <code>users</code>:</p>
  
  <pre><code>class Post &lt; ApplicationRecord
    has_many :assignments
    has_many :users, :through =&gt; :assignments
  end 
  
  class User &lt; ApplicationRecord
    has_many :assignments
    has_many :posts, :through =&gt; :assignments
  end 
  
  class Assignment &lt; ApplicationRecord
    belongs_to :request
    belongs_to :user
  end
  </code></pre>
  
  <p>Now in my association table (<code>assignment</code>) there are additional attributes for <code>creator:boolean</code> and <code>editor:boolean</code>. </p>
  
  <p>My question is what's the best way to set these secondary attributes from within the controller?</p>
  
  <p>Having looked around I've got a current solution:</p>
  
  <p><code>posts_controller.rb</code>:</p>
  
  <pre><code>class PostsController &lt; ApplicationController
    def create
      params.permit!
      @post = Post.new(post_params)
  
      if @post.save    
        Assignment.handle_post(@post.id, params[:creator], params[:editors])
        redirect_to posts_path, notice: "The post #{@post.title} has been created."
      else
        render "new"
      end
  end 
  </code></pre>
  
  <p><code>assignment.rb</code>:</p>
  
  <pre><code>class Assignment &lt; ApplicationRecord
    belongs_to :request
    belongs_to :user
  
    def self.handle_post(post_id, creator, assignment)
      Assignment.where(:post_id =&gt; post_id).delete_all
  
      Assignment.create!(:post_id =&gt; post_id, :user_id =&gt; creator, :creator =&gt; true, :editor =&gt; false)
  
      if editors.present?
        editors.each do |e| 
          Assignment.create!(:post_id =&gt; post_id, :user_id =&gt; e, :creator =&gt; false, :editor =&gt; true)
        end
      end
    end
  end
  </code></pre>
  
  <p>So what is essentially happening is I'm getting the user_ids from the form via <code>params</code> (<code>creator</code> returns 1 id, <code>editors</code> returns an array), and AFTER creating the post I'm deleting all columns associated with the post and recreating them off the new attributes.</p>
  
  <p>The issue I have here is I can't run <code>post</code> validations on these associations (e.g. check a creator is present). </p>
  
  <p>My two questions are as follows:</p>
  
  <ol>
  <li>Is this the correct way to handle secondary attributes? </li>
  <li>Is there a way to set the association up and then save it all at once so validations can be performed?</li>
  </ol>