【发布时间】:2008-11-12 21:32:26
【问题描述】:
鉴于以下情况,我如何在我的数据库中插入行? (或者我应该在我的架构中更正什么?)
型号:
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
我在尝试插入记录时浪费了几个小时,使用 rails 控制台进行测试。
【问题讨论】:
标签: ruby-on-rails model schema insert