wmoxam (owner)

Revisions

gist: 149169 Download_button fork
public
Public Clone URL: git://gist.github.com/149169.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Quiz < ActiveRecord::Base
  has_many :questions, :dependent => :destroy
end
 
class Question < ActiveRecord::Base
  belongs_to :quiz, :counter_cache => true
end
 
# Counter cache doesn't work in this case:
questions.each do |question|
  quiz.questions << question
end
assert_equal quiz.questions.size, quiz.questions.count # false!
 
# It does work in this one.
questions.each do |question|
  question.quiz = quiz
  question.save
end
assert_equal quiz.questions.size, quiz.questions.count # true!
 
# AR is inconsistent sometimes :(