Don't name columns like counter_cache columns

ActiveRecord has a feature called counter caching Show archive.org snapshot where the containing record in a has_many relationship caches the number of its children. E.g. when you have House has_many :rooms, Rails can cache the number of rooms in House#rooms_count.

Mind that when a model has a column that looks to Rails like a counter-cache column, Rails will apply counter-cache logic to your model, even if you're not using counter caches.

E.g. you have a house with 12 rooms, but house.rooms_count is 0 (for whatever reason). Now house.rooms will always be empty because Rails thinks that there is no point in querying the database.

A solution is to use another naming convention (like total_rooms).

Henning Koch Over 11 years ago