Creating joined records using has_many :through

2019-06-02 05:42发布

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:

  1. User provides data in a hash where key = the itemlist_id of what they want and the value = the quantity of that itemlist_id that they want. Let's say user wants two of 9, that would look like this: { 9 => 2}
  2. For each itemlist_id in the user provided hash, I'm going to look inside the Inventories table and pull out the inventory_ids where the itemlist_id matches what the user is looking for and the owner of that inventory_id is not the user him or herself. Let's say that in the Inventories table, there are 3 ids that fulfill this: [X, Y, Z]
  3. Now what I'd like to do is create the Transactions and associate the Transactions and Inventories 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 2 transactions 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

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"

2条回答
欢心
2楼-- · 2019-06-02 06:16

You are doing a wrong assignment in

transaction.inventories.create matched_inventory_id

And by the way it should be matched_inventory_ids because the ids method returns allways an array

The others.create method does not accept id or an id's array. It will accept attributes of the inventory in that case

To do association by id use the other_ids method

transaction.inventory_ids = transaction.inventory_ids + matched_inventory_ids 
查看更多
地球回转人心会变
3楼-- · 2019-06-02 06:20

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

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

It's a bit too much to specify class_name for each and every of your has_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 your has_many and keep it only for Transaction.transactories relation

查看更多
登录 后发表回答