Forum: Ruby on Rails counter_cache is not looking for children_count with acts_as

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3036c74b9c278dc525af5692080a6d80?d=identicon&s=25 Jeff Ward (Guest)
on 2006-06-08 06:55
(Received via mailing list)
Hi there, the acts_as_tree API says that I can set a counter cache and
that it will automatically increment the "children_count" column. I
did that but when I create a new page it asks for a "pages_count"
column instead.

Here is the relevant part of page.rb model:

class Page < ActiveRecord::Base
  acts_as_tree :order => :position, :counter_cache => true
  acts_as_list :scope => :parent_id
end

And here is what happens when I create a new page:

Mysql::Error: #42S22Unknown column 'pages_count' in 'field list':
UPDATE pages SET pages_count = pages_count + 1 WHERE (id = 2)

Any ideas why it's not asking for "children_count"?

Jeff
A43430351a6d4fb6d61d000bd56ed19a?d=identicon&s=25 Francois Paul (Guest)
on 2006-06-21 18:40
(Received via mailing list)
hi Jeff,
i'm noticing the same problem, have you by any chance figured it out?

:franc
A43430351a6d4fb6d61d000bd56ed19a?d=identicon&s=25 Francois Paul (Guest)
on 2006-06-21 19:08
(Received via mailing list)
ok, to answer my own question.
from http://dev.rubyonrails.org/ticket/1576

 >>
04/07/06 10:56:38: Modified by Frodo Larik
I was also a little confused by this, by thinking it was the name of the
table, but it isn't.

according to reflection.rb the name is made up like this:

            def counter_cache_column

                if options[:counter_cache] == true

                    "#{active_record.name.underscore.pluralize}_count"

                elsif options[:counter_cache]

                    options[:counter_cache]

                end

            end

that means if you set :counter_cache => true it uses a pluralized
version of the class name.

If you set :counter_cache => "whatever_counts" it uses the
"whatever_counts" column for counting.

Hope it helps.

 >>
This topic is locked and can not be replied to.