【问题标题】:How to insert rows when using a many-to-many relation使用多对多关系时如何插入行
【发布时间】: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


【解决方案1】:

(编辑:由于 ActiveRecord::Transactions,模型名称“Transaction”可能会给您带来一些问题。There is a lighthouse ticket。)

您的架构设置不正确。 “references”是“belongs_to”的别名。 Item 和 Transaction 不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设为单数)

您是否删除了数据库并重新运行迁移以构建新架构?

rake db:drop && rake db:create && rake db:migrate

这是我在控制台中得到的:

>> 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>

【讨论】:

  • 谢谢!现在我可以分别插入项目和事务(它们之间没有任何关系),这显然是错误的,但这是一个好的开始。不过,我还没有找到如何使这样的工作:item.transactions
  • 我在回迁并迁移到最新版本;我尝试了 drop-create-migrate ,然后完全运行了您运行的程序,但仍然得到关于 item= 未定义的完全相同的错误。难以置信...我想我会尝试像你说的那样重命名 Transaction。
  • 好的,我注意到通过我最近的测试,我已经复数了一些不应该的哈希,希望它能解决复数问题;删除数据库并修复剩余的多元化问题解决了一切。 (我也重命名了 Transaction。)我想我以后会关闭复数!
  • 再次感谢您的帮助,迈克。我还发现我可以做一个 i.transactions
  • 谢谢,兄弟帮了大忙。
【解决方案2】:

此架构应该为您提供您正在寻找的结果:

   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>

【讨论】:

    【解决方案3】:

    如果我理解正确的话。

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

    【讨论】:

    • 我将您的第二个 :name =&gt; "transaction" 替换为 :profit =&gt; 10,并在保存时得到:transactions.tran_items_id may not be NULL
    猜你喜欢
    • 2021-05-10
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    相关资源
    最近更新 更多