2008-11-12 40 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> 
相關問題