2

我正在开发一个使用多对多关系的 Rails 应用程序,它需要连接表上的额外属性(除了两个相关实体的 id)。

在模型中,一个产品可以属于多个订单,一个订单可以有多个产品。所以我创建了一个将产品分配给给定订单的操作,称为add_product_order,并且在发送表单时处理表单的控制器方法是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

orders_products 表(用于存储相关的 id 和数量)具有以下索引:

add_index :orders_products, [:order_id, :product_id], :unique => true

如果产品尚未在订单中(即OrdersProduct.new正在调用),则上述操作 (finish_add_product_order) 可以正常工作。但这是我在它已经存在时得到的错误,我尝试保存它

SQLite3::SQLException: no such column: orders_products.: UPDATE "orders_products" SET "quantity" = 4 WHERE "orders_products"."" IS NULL

请求参数如下,以防万一:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"TwPMk73MhCM2IeMfmf2g/fdm3+ahpyaxs1InULC/8ig=",
 "order"=>{"products"=>"4"},
 "quantity"=>["4"],
 "commit"=>"Update Order",
 "id"=>"2"}

我相信 WHERE 子句应该同时包含订单和产品 ID,以便识别受影响的行,但我不知道为什么不包含列名,也不知道为什么它以“IS NULL”结尾。

有什么建议么?谢谢

编辑

这是涉及的三个类的代码:

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

再次感谢

4

2 回答 2

0

将此 find 调用“ find_by_order_id_and_product_id”替换为“ where”调用,并通过调用获取返回对象的第一个对象Relationfirst.

  if @op = OrdersProduct.where(:order_id => @order.id, :product_id => @product.id).first
    @op.quantity = params['quantity'][0]
  else
    @op = OrdersProduct.new(:order => @order, :product => @product, :quantity => params['quantity'][0])
  end

让我知道这是否有帮助。

于 2012-10-07T07:09:28.977 回答
0

由于创建的 orders_products 表没有 id(带有create_table :orders_products, :id => :false do |t|...),所以我所要做的就是向它添加一个 id,通过这个迁移:

class AddIdToOrdersProduct < ActiveRecord::Migration
  def change
    add_column :orders_products, :id, :primary_key
  end
end

这样,它在做之后就起作用了 rake db:migrate

于 2012-10-08T03:01:12.063 回答