使用has_many:through创建连接记录

好吧我以为我很好地遵循了这个答案…… 如何将记录添加到has_many:通过rails中的关联 。 但显然不是。

型号代码:

class Transaction < ActiveRecord::Base belongs_to :request has_many :transactories has_many :inventories, through: :transactories end class Inventory < ActiveRecord::Base has_many :transactories has_many :transactions, through: :transactories end class Transactory < ActiveRecord::Base belongs_to :inventory belongs_to :transaction end 

我基本上试图将库存与交易相匹配(有东西的人,有想要那些东西的人)

这是我想要实现的流程:

  1. 用户在哈希中提供数据,其中key =他们想要的itemlist_id ,值=他们想要的itemlist_idquantity 。 假设用户想要9中的两个,看起来像这样: { 9 => 2}
  2. 对于用户提供的散列中的每个itemlist_id ,我将查看Inventories table并拉出inventory_ids ,其中itemlist_id与用户正在查找的itemlist_id相匹配,而inventory_id的所有者不是他或她自己的用户。 让我们说在Inventories table ,有3个ID可以实现这一点: [X, Y, Z]
  3. 现在我要做的是创建Transactions并将TransactionsInventories相互关联。 这一步的结果是双重的(我认为从视图的外观来看更容易编写:

    • 每个X,Y和Z inventory_id的所有者应该看到他们的项目有2个transactions (因此他们可以选择他们想要兑现的那个)
    • 用户可以看到,对于他们的两个transactions的每一个,都会向每个X,Y和Z的所有者发出通知

用于创建关联的代码

 # Assume overarching parent request has been created, called @requestrecord # Step 1, @transactionparams = { 9 => 2 } @transactionparams.each do |itemlist_id, quantity| # Step 2 matched_inventory_id = [X,Y,Z] matched_inventory_id = Inventory.where.not(signup_id: @requestrecord.signup.id).where(itemlist_id: itemlist_id).ids # Step 3, 2 transactions created each with itemlist_id of 9, each associated with inventory_ids X, Y, Z. In turn, inventory_ids X, Y, Z each associated with each of the two transactions created quantity.to_i.times do transaction = @requestrecord.transactions.create(itemlist_id: itemlist_id) transaction.inventories.create matched_inventory_id end end 

第3步让我沮丧。 代码卡在transaction.inventories.create行,我收到一个错误: uninitialized constant Transaction::Transactory

UPDATE

现在我You tried to define an association named transaction on the model TransactionInventory, but this will conflict with a method transaction already defined by Active Record. Please choose a different association name.了错误You tried to define an association named transaction on the model TransactionInventory, but this will conflict with a method transaction already defined by Active Record. Please choose a different association name. You tried to define an association named transaction on the model TransactionInventory, but this will conflict with a method transaction already defined by Active Record. Please choose a different association name. 我尝试指定class_namesforeign_keys ,请参阅下面的示例:

 belongs_to :transaction, foreign_key: "transaction_id", class_name: "Transaction" 

也许是一个加载问题,有可能在生产中你的代码没有任何修改就会因为急切加载而起作用。 无论如何你可以尝试这个来解决你的问题:

 class Transaction < ActiveRecord::Base belongs_to :request has_many :transactories, class_name: 'Transactories' has_many :inventories, through: :transactories end class Inventory < ActiveRecord::Base has_many :transactories, class_name: 'Transactories' has_many :transactions, through: :transactories end class Transactories < ActiveRecord::Base belongs_to :inventory, class_name: 'Inventory' belongs_to :transaction, class_name: 'Transaction' end 

像我建议的那样为每个has_many指定class_name有点太多了,但它的“安全”。

也许在您确认该解决方案有效之后,您应该能够从大多数has_many删除class_name并仅将其保留为Transaction.transactories关系

你正在做错的任务

 transaction.inventories.create matched_inventory_id 

顺便说一句,它应该是matched_inventory_ids因为ids方法总是返回一个数组

others.create方法不接受id或id的数组。 在这种情况下,它将接受库存的属性

要通过id进行关联,请使用other_ids方法

 transaction.inventory_ids = transaction.inventory_ids + matched_inventory_ids