Add all inventory transactions Rails

0

I am creating an inventory system, in which I have the following 2 models:

Product Model

class CreateProducts < ActiveRecord::Migration[5.2]
  def change
    create_table :products do |t|
      t.string :name
      t.float :price
      t.integer :weight
      t.string :unity

      t.timestamps
    end
  end
end

Inventory Transactions Model This registers the amount of items to enter or to leave the inventory for each product, for this I have added a Boolean field, which will determine the type of transaction, whether purchase or sale

class CreateInventoryTransactions < ActiveRecord::Migration[5.2]
  def change
    create_table :inventory_transactions do |t|
      t.string :invoice
      t.integer :product_id
      t.integer :quantity
      t.boolean :transaction_type

      t.timestamps
    end
  end
end

In this case I made a purchase transaction of 100 items of the same product, but at the time of trying to print that data of that product in the loop, it generates an error, and obvious given that this product has many transactions of purchase or sale, how would you print the sum of purchase transactions, or sales within the loop, for each product?

<% @products.each do |product| %>
  <tr>
    <td>-</td>
    <td><%= product.name %></td>
    <td><%= product.price %></td>
    <td><%= product.weight %></td>
    <td><%= product.unity %></td>
    <td><%= product.inventory_transactions.quantity %></td>
  </tr>
<% end %>

Avoiding the error of: undefined method 'quantity' for #<InventoryTransaction::ActiveRecord_Associations_CollectionProxy:0x007fdc3d68c038>

    
asked by Hector Hernandez 22.06.2018 в 20:07
source

1 answer

0

The problem is that you are calling the quantity method on a collection, not on a Product object.

What you have to do is first add the amounts of each transaction and then show them; for example:

product.inventory_transactions.sum(&:quantity)

Although you can use it like this directly in your view, I recommend you extract that logic and add it in the model Product :

class Product
  # ...

  def inventory_transactions_quantity
    inventory_transactions.sum(&:quantity)
  end
end

And in your view you can call the new method:

product.inventory_transactions_quantity
  

... when making a transaction I specify it by means of a Boolean field, that is, transaction_type , how could I subtract the amount of purchase data, that is, have transaction_type: true , minus the amount of transaction_type: false ?

You could add a method (private) in your model to add the values of quantity for each transaction_type and then use it in your current method to get the difference; for example:

# app/models/product.rb

class Product < ApplicationRecord
  # ...

  def inventory_transactions_quantity
    transactions_type_quantity(true) - transactions_type_quantity(false)
  end

  private
  def transactions_type_quantity(type)
    inventory_transactions.where(transaction_type: type).sum(&:quantity)
  end
end

As an exercise to improve this code, you might consider using scopes in the model InventoryTransaction to avoid using where within the model Product .

    
answered by 23.06.2018 / 01:44
source