Rails Counter Cache On the same model? -


i have model task, , each task has_many other tasks:

class task < activerecord::base   belongs_to :sub_task, class_name: task.name, touch: true   has_many :sub_tasks, class_name: task.name, foreign_key: :sub_task_id, dependent: :destroy end 

can add counter cache number of sub_tasks each task has? how?

yes can add counter cache.

class task < activerecord::base    belongs_to :sub_task, class_name: task.name, touch: true, counter_cache: :sub_tasks_count    has_many :sub_tasks, class_name: task.name, foreign_key: :sub_task_id, dependent: :destroy end 

you need create migration add new column named sub_tasks_count tasks table.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -