This project is archived and is in readonly mode.
counter_cache doesn't update when :order specified on has_many association
-
Mark Turner
Cant Duplicate In Rails 2.3.4
app/models/invoice_line_item.rb
class InvoiceLineItem < ActiveRecord::Base has_many :expenses, :order => "created_at ASC" endapp/models/expense.rb
class Expense < ActiveRecord::Base belongs_to :invoice_line_item, :counter_cache => true endConsole Session:
Loading development environment (Rails 2.3.4) >> ili = InvoiceLineItem.new +----------------+------------+------------+ | expenses_count | created_at | updated_at | +----------------+------------+------------+ | | | | +----------------+------------+------------+ 1 row in set >> ili.save InvoiceLineItem Create (0.8ms) INSERT INTO "invoice_line_items" ("created_at", "expenses_count", "updated_at") VALUES('2009-10-08 07:26:05', NULL, '2009-10-08 07:26:05') => true >> ili +----+----------------+-------------------------+-------------------------+ | id | expenses_count | created_at | updated_at | +----+----------------+-------------------------+-------------------------+ | 7 | | 2009-10-08 07:26:05 UTC | 2009-10-08 07:26:05 UTC | +----+----------------+-------------------------+-------------------------+ 1 row in set >> ili.expenses << Expense.new Expense Create (0.3ms) INSERT INTO "expenses" ("created_at", "updated_at", "invoice_line_item_id") VALUES('2009-10-08 07:26:46', '2009-10-08 07:26:46', 7) InvoiceLineItem Load (0.2ms) SELECT * FROM "invoice_line_items" WHERE ("invoice_line_items"."id" = 7) InvoiceLineItem Update (0.1ms) UPDATE "invoice_line_items" SET "expenses_count" = COALESCE("expenses_count", 0) + 1 WHERE ("id" = 7) Expense Load (0.4ms) SELECT * FROM "expenses" WHERE ("expenses".invoice_line_item_id = 7) ORDER BY created_at ASC +----+----------------------+-------------------------+-------------------------+ | id | invoice_line_item_id | created_at | updated_at | +----+----------------------+-------------------------+-------------------------+ | 5 | 7 | 2009-10-08 07:26:46 UTC | 2009-10-08 07:26:46 UTC | +----+----------------------+-------------------------+-------------------------+ 1 row in set >> ili.expenses +----+----------------------+-------------------------+-------------------------+ | id | invoice_line_item_id | created_at | updated_at | +----+----------------------+-------------------------+-------------------------+ | 5 | 7 | 2009-10-08 07:26:46 UTC | 2009-10-08 07:26:46 UTC | +----+----------------------+-------------------------+-------------------------+ 1 row in set >> ili.expenses.size => 1 >> ili.expenses.count SQL (0.3ms) SELECT count(*) AS count_all FROM "expenses" WHERE ("expenses".invoice_line_item_id = 7) => 1 >> ili.expenses_count => nil >> ili.save => true >> ili.reload InvoiceLineItem Load (0.3ms) SELECT * FROM "invoice_line_items" WHERE ("invoice_line_items"."id" = 7) +----+----------------+-------------------------+-------------------------+ | id | expenses_count | created_at | updated_at | +----+----------------+-------------------------+-------------------------+ | 7 | 1 | 2009-10-08 07:26:05 UTC | 2009-10-08 07:26:05 UTC | +----+----------------+-------------------------+-------------------------+ 1 row in set >> ili.expenses.size => 1 >> ili.expenses_count => 1 >> ili.expenses.count SQL (0.3ms) SELECT count(*) AS count_all FROM "expenses" WHERE ("expenses".invoice_line_item_id = 7) => 1 >> -
subimage
@Mark - read the original bug report. You missed the intent. It works when you use...
ili.expenses << Expense.newWhat doesn't work is when you use...
expense = Expense.create ili.expenses << expenseHowever if you remove the :order option then re-run the above code it does work for the already existing expense.
-
Rohit Arondekar
- State changed from new to stale
- Importance changed from to
Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.
-
Jeff Kreeftmeijer
Automatic cleanup of spam.