使用has_many:through创建联接记录 [英] Creating joined records using has_many :through
问题描述
Ok I thought I was following this answer pretty well... how to add records to has_many :through association in rails. But clearly not.
型号代码:
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
我基本上是在尝试将存货与交易(有货的人,或有货的人)进行匹配
I'm basically trying to match inventories with transactions (people who have stuff, with people who want that stuff)
这是我要实现的流程:
- 用户在哈希中提供数据,其中键=他们想要的
itemlist_id
,值=他们想要的itemlist_id
的quantity
.假设用户想要9个中的两个,如下所示:{ 9 => 2}
- 对于用户提供的哈希中的每个
itemlist_id
,我将查看Inventories table
的内部,并拉出inventory_ids
,其中itemlist_id
与用户要查找的内容以及该所有者的所有者inventory_id
不是用户他或她自己.假设在Inventories table
中,有3个ID可以满足此要求:[X, Y, Z]
-
现在我要做的是创建
Transactions
并将Transactions
和Inventories
相互关联.此步骤的结果有两个方面(我认为从视图的外观来看更容易编写:
- User provides data in a hash where key = the
itemlist_id
of what they want and the value = thequantity
of thatitemlist_id
that they want. Let's say user wants two of 9, that would look like this:{ 9 => 2}
- For each
itemlist_id
in the user provided hash, I'm going to look inside theInventories table
and pull out theinventory_ids
where theitemlist_id
matches what the user is looking for and the owner of thatinventory_id
is not the user him or herself. Let's say that in theInventories table
, there are 3 ids that fulfill this:[X, Y, Z]
Now what I'd like to do is create the
Transactions
and associate theTransactions
andInventories
with each other. The outcome of this step is two-fold (I think it's easier to write from the perspective of what the view will look like:
- 每个X,Y和Z
inventory_id
的所有者应看到他们的物品有2个transactions
(以便他们可以选择要尊敬的一个) - 用户可以看到他们的2个
transactions
中每个X,Y和Z的所有者都收到了通知
- that the owner of each X, Y, and Z
inventory_id
should see that there are 2transactions
for their item (so they can pick which one they want to honor) - that the user can see that for each of their 2
transactions
, there are notifications to the owners of each X, Y, and 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
Step 3 is tripping me up. The code is stuck at the transaction.inventories.create
line and I'm getting an error: uninitialized constant Transaction::Transactory
更新
现在,我陷入了错误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_names
和foreign_keys
,请参见以下示例:
Now I'm stuck at the error 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.
I tried specifying class_names
and foreign_keys
, see example below:
belongs_to :transaction, foreign_key: "transaction_id", class_name: "Transaction"
推荐答案
您在
transaction.inventories.create matched_inventory_id
顺便说一句,它应该是matched_inventory_ids
,因为ids
方法始终返回一个数组
And by the way it should be matched_inventory_ids
because the ids
method returns allways an array
others.create
方法不接受id或id的数组.在这种情况下,它将接受广告资源的属性
The others.create
method does not accept id or an id's array. It will accept attributes of the inventory in that case
要使用id进行关联,请使用other_ids
方法
To do association by id use the other_ids
method
transaction.inventory_ids = transaction.inventory_ids + matched_inventory_ids
这篇关于使用has_many:through创建联接记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!