Ok I thought I was following this answer pretty well... how to add records to has_many :through association in rails. But clearly not.
Model code:
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)
Here's the flow I'm trying to achieve:
- 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:- 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
- that the owner of each X, Y, and Z
Code to create the associations
# 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
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
UPDATE
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"
You are doing a wrong assignment in
And by the way it should be
matched_inventory_ids
because theids
method returns allways an arrayThe
others.create
method does not accept id or an id's array. It will accept attributes of the inventory in that caseTo do association by id use the
other_ids
methodPerhaps a loading issue, it is possible that in production your code without any modification would work because of eager loading. Anyhow you could try this to fix your issue:
It's a bit too much to specify
class_name
for each and every of yourhas_many
like I suggest, but its "safe".And perhaps after you confirm that solution works you should be able to remove
class_name
from most of yourhas_many
and keep it only forTransaction.transactories
relation