title: How to save in a proper way a HABTM relationship on Rails
body: <p>I actually need some advice from advanced Ruby/Rails developers on what's the common and proper way to save a <code>has_and_belongs_to_many</code> association?</p>
  
  <p>Here is what I did, but to me it feels really dirty and I don't really like to see that code in my app :)</p>
  
  <p><strong>models/company.rb</strong></p>
  
  <pre><code>class Company &lt; ActiveRecord::Base
    has_and_belongs_to_many :type_taxes
  
    ## Add Type taxes association
    def insert_types_taxes( types_taxes )
      self.type_taxes.clear
      self.type_taxes &lt;&lt; types_taxes
    end
  end
  </code></pre>
  
  <p><strong>models/taxe.rb</strong></p>
  
  <pre><code>class Taxes &lt; ActiveRecord::Base
    has_and_belongs_to_many :societes
  end
  </code></pre>
  
  <p><strong>controllers/societes_controller</strong></p>
  
  <pre><code>class SocietesController &lt; ApplicationController
    # I use basically the same code for the update method
    def create
      params[:societe][:type_taxes_ids] ||= []
      @societe = Societe.new( societe_params )
      @societe.user_id = current_user.id
      if @societe.save
        add_types_taxes_to_societe
        redirect_to societes_path
      else
        load_resources
        render :new
      end
    end
  
    private
      def add_types_taxes_to_societe
        types_taxes = TypeTaxe.find params[:societe][:type_taxe_ids]
        @societe.insert_types_taxes types_taxes
      end
  end
  </code></pre>
  
  <p>When this work fine, it really feels dirty to me. It also doesn't tell me if the associations creation has been created with success or no.</p>
  
  <p>Any suggestions are more than welcome to improve my skills and my code :)</p>
  
  <p>Thanks</p>