I am developing a Rails application which uses a many-to-many relationship, which needs extra attributes on the join table (apart from the ids of the two related entities).
In the model, a Product can belong to many Orders, and an Order can have many Products.
So I created an action for assigning Products to a given Order, called add_product_order, and the controller method that handles the form when it’s sent is finish_add_product_order:
def finish_add_product_order
@order = Order.find(params[:id])
@product = Product.find(params[:order][:products])
if @op = OrdersProduct.find_by_order_id_and_product_id(@order.id, @product.id)
@op.quantity = params['quantity'][0]
else
@op = OrdersProduct.new(:order => @order, :product => @product, :quantity => params['quantity'][0])
end
respond_to do |format|
if @op.save
format.html { redirect_to @order, notice: 'Order was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "add_product_order", notice: 'No se ha podido grabar.' }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
The orders_products table (which is used for storing the related ids and quantities) has the following index:
add_index :orders_products, [:order_id, :product_id], :unique => true
The action above (finish_add_product_order) works fine if the Product isn’t already in the Order (i.e. OrdersProduct.new is being called). But this is the error I get when it already exists and I try to save it:
SQLite3::SQLException: no such column: orders_products.: UPDATE "orders_products" SET "quantity" = 4 WHERE "orders_products"."" IS NULL
The request parameters are the following, just in case:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"TwPMk73MhCM2IeMfmf2g/fdm3+ahpyaxs1InULC/8ig=",
"order"=>{"products"=>"4"},
"quantity"=>["4"],
"commit"=>"Update Order",
"id"=>"2"}
I believe the WHERE clause should include both the order and the product id so as to identify the row affected, but I don’t know why the column names are not being included, neither why it ends with “IS NULL”.
Any suggestions? Thanks
EDIT
This is the code for the three classes involved:
class Order < ActiveRecord::Base
belongs_to :user
has_many :gardens, :dependent => :destroy
has_many :products, :through => :orders_products
has_many :orders_products
attr_accessible :address, :delivery_date, :paid, :user, :orders_products_attributes
# hid some irrelevant methods
end
class Product < ActiveRecord::Base
has_many :gardens, :through => :gardens_products
has_many :gardens_products
has_many :orders, :through => :orders_products
has_many :orders_products
attr_accessible :cost, :description, :stock, :title, :x_size, :y_size, :pivot_x_position, :pivot_y_position, :is_gallery_product
validates_presence_of :cost, :stock, :title, :x_size, :y_size, :pivot_x_position, :pivot_y_position
validates_numericality_of :cost, :stock, :x_size, :y_size, :pivot_x_position, :pivot_y_position, :only_integer => true, :message => "Solo puede ser un numero entero"
validates_inclusion_of :cost, :in => 0..1000000, :message => "Solo puede estar entre 0 y 1 000 000"
validates_inclusion_of :stock, :in => 0..10000000, :message => "Solo puede estar entre 0 y 10 000 000"
validates_inclusion_of :x_size, :y_size, :in => 1..200, :message => "Solo puede estar entre 0 y 200"
# Poner el rango variable en los pivotes (dentro del rango del tamano)
validates_inclusion_of :pivot_x_position, :in => 1..200, :message => "El pivote debe estar dentro de las dimensiones del producto"
validates_inclusion_of :pivot_y_position, :in => 1..200, :message => "El pivote debe estar dentro de las dimensiones del producto"
validates :title, :length => { :minimum => 5},
:presence => { :message => "Debe tener un titulo de largo mayor que 5 caracteres" }
end
class OrdersProduct < ActiveRecord::Base
belongs_to :order, :dependent => :destroy
belongs_to :product
attr_accessible :order, :product, :quantity
validates_numericality_of :quantity,
:only_integer => true,
:message => "solo puede ser un numero entero"
validates_inclusion_of :quantity,
:in => 1..1000,
:message => "solo puede estar entre 1 y 1 000"
end
Thanks again
Since the orders_products table had been created without an id (with
create_table :orders_products, :id => :false do |t|...), all I had to do was to add an id to it, with this migration:that way, it worked after doing
rake db:migrate.