I'm using rspec for testing and hornsby scenarios for object graphs used in tests.
Is it good practice to initialize counter cache columns to 0 value instead of leaving them uninitialized (nil)? Or should i define default value in migrations that create those counter cache columns?
Yes, you should set the default value. Otherwise you have to special case math operations to handle NULLs.
Let's say you had an array of post objects and you wanted to get sum the number of comments.
If you initialize to zero @posts.sum(&:comment_count)
will, but if you don't it might not because it will fail on nil.
I recommend defining your column like this:
add_column :posts, :comments_count, :integer, :default => 0, :null => false