【发布时间】:2015-06-08 21:35:19
【问题描述】:
我有这种情况:
class Order < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :orders
end
class CreateJoinTableOrdersProducts < ActiveRecord::Migration
def change
create_join_table :orders, :products do |t|
t.index [:order_id, :product_id]
t.index [:product_id, :order_id]
t.decimal :price, precision: 8, scale: 2
t.integer :quantity, default: 1
end
end
end
现在,我需要使用命令行添加一些记录:
这很好用:
Order.first.products << Product.first
但是,我需要添加更多字段,例如:price、quantity...
我该怎么做?
【问题讨论】:
-
在这种情况下你应该使用
has_many :through -
我认为你会反对 Rails。 :) 阅读stackoverflow.com/questions/2327849/…。现在,如果您按照链接答案的说明进行操作,您将从 Rails free 获得工具。
标签: ruby-on-rails ruby