鉴于以下情况,我如何在我的数据库中插入行? (或者我应该在我的架构是否正确?)如何在使用多对多关系时插入行
型号:
class Item < ActiveRecord::Base
has_many :tran_items
has_many :transactions, :through => :tran_items
end
class TranItem < ActiveRecord::Base
belongs_to :item
belongs_to :transaction
end
class Transaction < ActiveRecord::Base #answer: rename Transaction
has_many :tran_items
has_many :items, :through => :tran_items
end
模式:
create_table :items do |t|
t.references :tran_items #answer: remove this line
t.string :name
end
create_table :tran_items do |t|
t.belongs_to :items, :transactions, :null => false #answer: unpluralize
t.integer :quantity
end
create_table :transactions do |t|
t.references :tran_items #answer: remove this line
t.decimal :profit
end
我失去了几个小时试图插入记录,使用轨道控制台来测试的东西出。
看看这个帖子http://stackoverflow.com/questions/11665389/how-to-save-to-database-with-associations- 11681454#11681454 – juliangonzalez 2012-07-27 05:11:55