Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No association found for name - Nested Form Rails 4

I have problems with has_many / has_many relationship using nested form for the following case.

I have articles and this can have many units (Kg, bags, etc). And through my form I need to record these data.

So I decided to use nested-form with relations has_many but I have a error: ArgumentError (No association found for name 'article_unit_of_measurements'. Has it been defined yet?)

This is my code of Classes:

class Article < ActiveRecord::Base
    has_many :unit_of_measurements, :through => :article_unit_of_measurements
    accepts_nested_attributes_for :article_unit_of_measurements
end

class ArticleUnitOfMeasurement < ActiveRecord::Base
    belongs_to :article
    belongs_to :unit_of_measurement

    accepts_nested_attributes_for :unit_of_measurement
end


class UnitOfMeasurement < ActiveRecord::Base
    has_many :articles, :through => :article_unit_of_measurements
end

And in my form using jquery plugin select2.js to show my selection units as tags.

= simple_form_for([:shop, @article], html: {class: 'form-horizontal' }) do |f|
  .col-md-10
    = f.input :name, placeholder: "Name", :input_html => {:class => 'form-control'}, label: false
  %label.col-md-2.control-label{for: "unit_of_measurement_id"} Unidad de medida
    .col-md-10
      %select#unit-select-measure.select2{:name =>"article[article_unit_of_measurements_attributes][#{Time.now.to_s}][unit_of_measurement_id]", :style => "width:100%;;padding: 0;border: none;", :multiple => true}
        - @unitOfMeasurement.each do |unit|
           %option{:value => "#{unit.id}"}
             = unit.name
   %button.btn.btn-primary{type: "submit"} Guardar
:javascript
  $('#unit-select-measure').select2();

And this is my controller:

class Shop::ArticlesController < ApplicationController
 def new
    @article = Article.new
    @unitOfMeasurement = UnitOfMeasurement.all
    render layout: false
  end

  def create
    article = Article.new(article_parameters)
    if article.save
      flash[:notice] = "Lorem ipsum dolor sit amet."
      redirect_to :action => :index
    else
      flash[:error] = "Lorem ipsum dolor sit amet."
      redirect_to :action => :index
    end
  end

  private
  def article_parameters
    params.require(:article).permit(:name, article_unit_of_measurements_attributes: [:id, :article_id, :unit_of_measurement_id])
  end
end

P.D: Data units already populated

like image 324
Dvex Avatar asked Mar 07 '14 04:03

Dvex


1 Answers

Replace

class Article < ActiveRecord::Base
    has_many :unit_of_measurements, :through => :article_unit_of_measurements
    accepts_nested_attributes_for :article_unit_of_measurements
end

with

class Article < ActiveRecord::Base
    has_many :article_unit_of_measurements   ## Added association
    has_many :unit_of_measurements, :through => :article_unit_of_measurements
    accepts_nested_attributes_for :article_unit_of_measurements
end

You missed the has_many association with the article_unit_of_measurements.

Hence, the error ArgumentError (No association found for name 'article_unit_of_measurements'

like image 143
Kirti Thorat Avatar answered Oct 17 '22 03:10

Kirti Thorat