I have created a simple set of two related tables of 'order' in which many 'line_times' are related to a line item The quantity is also, like
order 1
line_a a: 'knitting basket for beginners', quantity: 3
line_itum b: 'a dummy guide for vampirism' quantity: 1
When I install the migration, then I can use the amount in it:
Order.find (: all) .each do | O | O.update_attribute: line_items_count, o.line_items.map (& amp; quantity) .sum end which gives me the correct number (4), but I do this on this order model Because I'm unable to exceed line item volume, and so this line calculates the number of items (2).
Therefore, line_item is in model I:
related_ to: command,: counter_cache = & gt; Is true Is there any way I can specify the quantity so that it's right to say 4 instead of 2?
The feature 'counter_cache` is for maintaining counting of dependent objects (not yoga).
You can easily get it by writing a few lines of Ruby Code.
Assume that your order has the line_items_sum column in the table. The value of this column should be a default of 0. class AddLineItemsSumToOrder & lt; ActiveRecord :: Migration DF self.up add_column: command,: line_items_sum,: integer,: default => 0 end DEIF self.down removal_column: command, line_items_sum end-end class command & lt; ActiveRecord :: Base is_my: line_time end Now add callback to the LineItem class. class LineItem
Comments
Post a Comment