title: Trouble on updating record using an 'has_many :through' Record Association
body: <p>I am using Ruby on Rails 3.0.7 and I have a problem on an associated model validation for an <em>updating</em> controller action. I am using an "has_many :through" association so that I can handle user-category relationships. </p>
  
  <p>In the <code>User</code> model I have:</p>
  
  <pre><code>has_many :user_category_relationships,
    :autosave    =&gt; true,
    :dependent   =&gt; :destroy
  has_many :user_categories,
    :through     =&gt; :user_category_relationships,
    :source      =&gt; :user_category,
    :dependent   =&gt; :destroy
  </code></pre>
  
  <p>In the <code>_form</code> view file I have:</p>
  
  <pre><code>&lt;% @current_user.user_categories.each do |user_category| %&gt;
    &lt;%= check_box_tag :user_category_ids, user_category.id, @user.user_categories.include?(user_category), :name =&gt; 'user_relationships[user_category_ids][]' %&gt;
    &lt;%= label_tag "user_category_#{user_category.id}", user_category.name %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>In the <code>UserCategoryRelationship</code> model I have:</p>
  
  <pre><code>belongs_to :users
  belongs_to :categories
  
  # Validation
  validates :name_id,
    :presence   =&gt; true,
    :uniqueness =&gt; {
      :scope          =&gt; [:created_by_user_id, :category_id]
    }
  validates :category_id,
    :presence   =&gt; true,
    :uniqueness =&gt; {
      :scope          =&gt; [:created_by_user_id, :name_id]
    }
  validates :created_by_user_id,
    :presence   =&gt; true,
    :uniqueness =&gt; {
      :scope          =&gt; [:category_id, :name_id]
    }
  </code></pre>
  
  <p>In the <code>UsersController</code> I have:</p>
  
  <pre><code>def create
    ...
  
    # `params[:user_relationships][:user_category_ids]` used below are id values 
    # related to user-category relationships.
  
    unless params[:user_relationships][:user_category_ids]
      # Set default user category if not selected.
      @user.user_category_relationships.build(
        :category_id        =&gt; '1',
        :created_by_user_id =&gt; @current_user.id,
        :name_id            =&gt; @name.id
      )
    else
      params[:user_relationships][:user_category_ids].each { |user_category_id|
        @user.user_category_relationships.build(
          :category_id        =&gt; user_category_id,
          :created_by_user_id =&gt; @current_user.id,
          :name_id            =&gt; @name.id
        )
      }
    end
  
    if @user.save
      ...
    end
  end
  
  def update
    ...
  
    # Note: The following code is equal to that in the controller 'create' 
    # action. However here is my problem because validation (read after 
    # for more information about).
  
    unless params[:user_relationships][:user_category_ids]
      # Set default user category if not selected.
      @user.user_category_relationships.build(
        :category_id        =&gt; '1',
        :created_by_user_id =&gt; @current_user.id,
        :name_id            =&gt; @name.id
      )
    else
      params[:user_relationships][:user_category_ids].each { |user_category_id|
        @user.user_category_relationships.build(
          :category_id        =&gt; user_category_id,
          :created_by_user_id =&gt; @current_user.id,
          :name_id            =&gt; @name.id
        )
      }
    end
  
    if @user.update_attributes(params[:user])
      ...
    end
  end
  </code></pre>
  
  <p>The above code works except for the <code>update</code> action: when I try to run that I get always the same error, <em>as well</em>.</p>
  
  <pre><code># Validation errors on updating the 'user_category_relationships' associated model
  :"user_category_relationships.category_id"        =&gt;["has already been taken"]
  :"user_category_relationships.name_id"            =&gt;["has already been taken"]
  :"user_category_relationships.created_by_user_id" =&gt;["has already been taken"]
  </code></pre>
  
  <p><em>What I would like to do</em>, since I am not using the "RoR magical\automatic way" at all (that is, I don't use the <a href="http://guides.rubyonrails.org/association_basics.html#has_many-collection_singular" rel="nofollow noreferrer"><code>collection_singular_ids</code></a> method - see also <a href="http://millarian.com/programming/ruby-on-rails/quick-tip-has_many-through-checkboxes/" rel="nofollow noreferrer">this</a> and <a href="https://stackoverflow.com/questions/6582488/adding-more-data-on-saving-has-many-through-associated-records-by-keeping-use">this</a>), <em>is to make properly work the <code>update</code> controller action so that I can still use the "automatic" creation of user-category relationships</em> (triggered on <code>save</code> and <code>update_attributes(...)</code> methods) <em>but avoiding to generate above validation errors</em> (that is, create valid data in the database table). <strong>How can I do? What do you advice about this issue?</strong></p>
  
  <p>P.S.: Where I am in trouble is in the <code>update</code> action, precisely figuring out how to "choose"\"filter" and "coding" records to update, to create and to delete in the database having params[:user_relationships][:user_category_ids] as input data.</p>