Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting an Association type Mismatch?

I am new to RoR and have run into a problem that has been driving me crazy, which I can't figure out!

Basically, I have created a product form and added a 'Supplier' select field, which finds all my suppliers but now when I try to update a current product or create a new one, I get the following error:

ActiveRecord::AssociationTypeMismatch in ProductsController#create
Supplier(#2178099880) expected, got String(#2148287480)

I understand what the error is telling me, but I don't understand why. I have also created an association for product category the same way, which works fine!

Also, if I add a validation to validate the presence of supplier, I get a 'supplier can't be blank' error, even if a supplier is selected.

Would really appreciate some help on this!

In my _form.html I have:

<%= f.label :supplier %>
<%= f.select(:supplier, Supplier.find(:all).collect {|c| [ c.name, c.id ]}, :include_blank => true) %>

Products Controller:

class ProductsController < ApplicationController
  # GET /products
  # GET /products.xml
  def index
    @products = Product.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @products }
    end
  end

  # GET /products/1
  # GET /products/1.xml
  def show
    @product = Product.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @product }
    end
  end

  # GET /products/new
  # GET /products/new.xml
  def new
    @product = Product.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @product }
    end
  end

  # GET /products/1/edit
  def edit
    @product = Product.find(params[:id])
  end

  # POST /products
  # POST /products.xml
  def create
    @product = Product.new(params[:product])

    respond_to do |format|
      if @product.save
        format.html { redirect_to(@product, :notice => 'Product was successfully created.') }
        format.xml  { render :xml => @product, :status => :created, :location => @product }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /products/1
  # PUT /products/1.xml
  def update
    @product = Product.find(params[:id])

    respond_to do |format|
      if @product.update_attributes(params[:product])
        format.html { redirect_to(@product, :notice => 'Product was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.xml
  def destroy
    @product = Product.find(params[:id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to(products_url) }
      format.xml  { head :ok }
    end
  end
end

Product Model:

class Product < ActiveRecord::Base

  belongs_to :supplier
  belongs_to :categories
  default_scope :order => 'product_number'

  validates_presence_of :product_name, :product_number, :category, :opening_order, :initial_cover,
  :country_of_origin, :supplier

  validates_uniqueness_of :product_number

  # Paperclip

  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
like image 390
DreLambo Avatar asked Dec 21 '22 11:12

DreLambo


2 Answers

Instead of creating form fields for supplier try creating them for supplier_id:

<%= f.label :supplier_id %>
<%= f.select(:supplier_id, Supplier.find(:all).collect {|c| [ c.name, c.id ]}, :include_blank => true) %>
like image 62
Wizard of Ogz Avatar answered Jan 02 '23 05:01

Wizard of Ogz


Reporting yet another reason this may happen: In my case I was accepting attributes in the creation form of the resource, but I forgot to add "accepts_nested_attributes_for" in the parent model.. Hope it helps!

like image 24
yara Avatar answered Jan 02 '23 05:01

yara