【发布时间】:2019-01-24 13:00:03
【问题描述】:
我有一个 has_many :questions 的用户模型和一个 belongs_to :user 和 belongs_to :expert, as polymorphic: true 的问题。专家可以是培训师或医生
我需要为 User 以及 Trainer 和 Doctor 设置一个计数器缓存。
class Question < ApplicationRecord
belongs_to :user
belongs_to :expert, polymorphic: true
has_many :answers, dependent: :destroy
end
class User
has_many :questions
has_many :answers, as: :responder
end
class Trainer
has_many :questions, as: :expert
end
class Doctor
has_many :questions, as: :expert
end
是否可以同时为belongs_to :user 和belongs_to :expert, polymorphic: true 设置counter_cache?
这样我可以同时做到user.questions_count 和trainer.questions_count
【问题讨论】:
标签: ruby-on-rails associations