Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you should rebuild this associations and models</p> <p>What I'd do is to have this models (a little bit different than yours):</p> <p>Salesman</p> <pre><code>class Salesman &lt; ActiveRecord::Base attr_accessible :last_name, :name has_many :sales end </code></pre> <p>Product</p> <pre><code>class Product &lt; ActiveRecord::Base attr_accessible :name, :qty has_many :product_sales end </code></pre> <p>Sale</p> <pre><code>class Sale &lt; ActiveRecord::Base belongs_to :salesman has_many :product_sales end </code></pre> <p>And here's the trick</p> <p>Model ProductSale</p> <pre><code>class ProductSale &lt; ActiveRecord::Base belongs_to :sale belongs_to :product attr_accessible :qty after_create :decrement_stock def decrement_stock self.product.update_attribute("qty", (product.qty - self.qty)) end end </code></pre> <p>There you have Sale that <code>belongs_to</code> salesman.</p> <p>Sale <code>has_many</code> <code>product_sales</code></p> <p>and <code>product_sales</code> belongs_to <code>product</code> and <code>sales</code> that way in one sale you have multiple product</p> <p>Then when a <code>ProductSale</code> is saved there's the callback <code>after_create :decrement_stock</code></p> <p>And you can update the attribute for that specific product.</p> <p>Remember that you'll have a quantity for each product and not for the whole sale.</p> <p>Here's a little example I set up for you:</p> <p><a href="https://github.com/Mr-Nizzle/inventory" rel="nofollow">https://github.com/Mr-Nizzle/inventory</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload