Q:

Undefined method build, build has_many association in rails 3.2

I have the following problem:
I have to create a new associated row in my database.
I have:
companies table

references table

Companies has_many References.
References belongs_to Company.
In my Company table i have the following attribute:
id | city | email | ...|

In my Reference table i have the following attribute:
id | name | surname | company_id | ...|

Company.rb model:
belongs_to :references

Reference.rb model:
has_many :companies

In Company controller:
  def create

    @company = Company.new(company_params)
    @company.references.build(params[:company][:email_r])   
        respond_to do |format|  
      if @company.save
        format.html { redirect_to companies_index_path, :notice => 'created'}
        format.json { render :json => companies_index_path, :status => :created, :location => @company }
      else
        format.html { render :action => "new" }
        format.json { render :json => @company.errors, :status => :unprocessable_entity }
      end
    end
  end

My error is:
undefined method `build' for nil:NilClass

A:

According to your table schema, associations should be like - company has many references and reference belong to a company. In Company.rb it should be like below - 
class Company
  has_many :references
end 

And in Reference.rb it should be like 
class Reference 
   belongs_to :company
end

