如何在使用多对多关系时插入行

时间:2022-10-04 12:01:33

Given the following, how could I insert rows in my db? (Or what should I correct in my schema?)

鉴于以下内容,我如何在我的数据库中插入行? (或者我应该在我的架构中纠正什么?)

Models:

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

Schema:

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

I lost a few hours trying to insert records, using the rails console to test things out.

我试图插入记录几个小时,使用rails控制台来测试。

3 个解决方案

#1


6  

(edit: the model name "Transaction" may cause you some problems due to ActiveRecord::Transactions. There is a lighthouse ticket.)

(编辑:模型名称“交易”可能会因ActiveRecord :: Transactions而导致一些问题。有一个灯塔票。)

Your schema is not set up correctly. "references" is an alias to "belongs_to". Item and Transaction don't belong_to trans_items, they each has_many trans_items (according to your models)

您的架构未正确设置。 “references”是“belongs_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

(edit: make belongs_to singular)

(编辑:make belongs_to singular)

Did you drop the db and re-run the migrations to build the new schema?

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

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

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

Here is what I get in the console:

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

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

#2


1  

If I understood correctly.

如果我理解正确的话。

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

#3


1  

This schema should give you the results you are looking for:

此架构应该为您提供所需的结果:

   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

Here are the models:

以下是模型:

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

Now using the console:

现在使用控制台:

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

#1


6  

(edit: the model name "Transaction" may cause you some problems due to ActiveRecord::Transactions. There is a lighthouse ticket.)

(编辑:模型名称“交易”可能会因ActiveRecord :: Transactions而导致一些问题。有一个灯塔票。)

Your schema is not set up correctly. "references" is an alias to "belongs_to". Item and Transaction don't belong_to trans_items, they each has_many trans_items (according to your models)

您的架构未正确设置。 “references”是“belongs_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

(edit: make belongs_to singular)

(编辑:make belongs_to singular)

Did you drop the db and re-run the migrations to build the new schema?

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

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

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

Here is what I get in the console:

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

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

#2


1  

If I understood correctly.

如果我理解正确的话。

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

#3


1  

This schema should give you the results you are looking for:

此架构应该为您提供所需的结果:

   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

Here are the models:

以下是模型:

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

Now using the console:

现在使用控制台:

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