2008-11-12 44 views
3

鉴于以下情况,我如何在我的数据库中插入行? (或者我应该在我的架构是否正确?)如何在使用多对多关系时插入行

型号:

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 

我失去了几个小时试图插入记录,使用轨道控制台来测试的东西出。

+0

看看这个帖子http://stackoverflow.com/questions/11665389/how-to-save-to-database-with-associations- 11681454#11681454 – juliangonzalez 2012-07-27 05:11:55

回答

6

(编辑:型号名称“交易”可能会导致您因的ActiveRecord ::交易的一些问题There is a lighthouse ticket。)

你的架构设置不正确。 “引用”是“belongs_to”的别名。商品和交易不belong_to trans_items,他们每个的has_many trans_items(根据您的机型)

create_table :items do |t| 
    t.string  :name 
end 
create_table :tran_items do |t| 
    t.belongs_to :item, :transaction, :null => false 
    t.integer :quantity 
end 
create_table :transactions do |t| 
    t.decimal :profit, :default => 0 
end 

(编辑:使belongs_to的奇)

你放下数据库,并重新运行迁移来构建新的模式?

耙分贝:滴& &耙分贝:创建& &耙分贝:迁移

以下是我在控制台中看到:

>> i = Item.create(:name => 'My Item')  
=> #<Item id: 2, name: "My Item"> 
>> t = Transaction.create(:profit => 100) 
=> #<Transaction id: 2, profit: #<BigDecimal:2411d2c,'0.1E3',4(8)>> 
>> t.tran_items.create(:item => i) 
=> #<TranItem id: nil, item_id: 2, transaction_id: 2, quantity: nil> 
+0

谢谢!现在我可以单独插入项目和事务(它们之间没有任何关系),这显然是错误的,但是一个好的开始。尽管如此,我还没有找到如何做这样的工作:item.transactions << Transaction.create(...) – 2008-11-13 15:56:36

1

如果我理解正确。

item = Item.new(:name => "item") 
item.transactions.build(:name => "transaction") 
item.save! 
+0

我将`:name =>“transaction”替换为`:profit => 10`,并在保存时得到了这个:`transactions.tran_items_id可能不是NULL` – 2008-11-12 21:44:08

1

这个模式应该给你你正在寻找的结果用于:

create_table :items do |t| 
     t.string  :name 
    end 
    create_table :purchase_items do |t| 
     t.belongs_to :item, :purchase, :null => false 
     t.integer :quantity 
    end 
    create_table :purchases do |t| 
     t.decimal :profit,    :default => 0 
    end 

这里是型号:

class Purchase < ActiveRecord::Base 
    has_many       :purchase_items 
    has_many :items, :through =>  :purchase_items 
end 

class Item < ActiveRecord::Base 
    has_many       :purchase_items 
    has_many :purchases, :through => :purchase_items 
end 

class PurchaseItem < ActiveRecord::Base 
    belongs_to :item 
    belongs_to :purchase 
end 

现在使用控制台:

>> i = Item.create(:name => 'Item One') 
=> #<Item id: 1, name: "Item One"> 
>> p = Purchase.create(:profit => 100) 
=> #<Purchase id: 1, profit: #<BigDecimal:2458cf4,'0.1E3',4(8)>> 
>> p.purchase_items.create(:item => i) 
=> #<PurchaseItem id: 1, item_id: 1, purchase_id: 1, quantity: nil> 
相关问题