我想知道 counter_cache 是否可以在单表继承中工作。
对于这些模型:
class User
has_many :questions
end
class Question
belongs_to :user, :counter_cache => true
end
class SimpleQuestion < Question
end
class ComplexQuestion < Question
end
那么以下计数器会起作用吗?
create_table(:users) do |t|
t.integer :questions_count
t.integer :simple_questions_count
t.integer :complex_questions_count
end
- 他们都工作
- 他们都没有工作
- 只
questions_count
工作 - 只有
simple_questions_count
和complex_questions_count
哪一个?我猜是第 3 个,但我还想要 4 个。如果不是 4,我如何使 4 起作用?
=== 更新 ===
这是一个例子:
id, user_id, question_content, type
1, 3, something, SimpleQuestion
2, 3, something, SimpleQuestion
3, 3, something, ComplexQuestion
所以现在我想要:
user.questions_count # => 3
user.simple_questions_count # => 2
user.complex_questions_count # => 1
我的问题是,单表继承的基本行为是什么:counter_cache => true
,是否可以应用于单表继承?