has_many :through with counter_cache

后端 未结 4 987
悲&欢浪女
悲&欢浪女 2020-12-06 09:40

It is my understanding that when defining a :counter_cache option it is to be specified on the model that includes the belongs_to declaration. So I am a little unsure of how

4条回答
  •  悲&欢浪女
    2020-12-06 10:01

    I added a counter_cache to a has_many :through association on Rails 5.1, and the philosophy is the same as with has_many. Using the Physician, Appointment, Patient example:

    1. add patients_count to the physicians table as an integer
    2. add a counter cache to the the join model (appointment.rb): belongs_to :physician, counter_cache: :patients_count

    Note: the answer above is correct, this answer just confirms that it works on Rails 5.1.

提交回复
热议问题