Hi all,
I’m currently working through Simply Rails 2 and have come across a problem when trying to implement the Counter Cache in chapter 9.
After following the counter cache instructions (which I’ve now gone through 3 times! and checked against the source code from SitePoint) I get this error when attempting to load the Stories page in Shovell
ActiveRecord::StatementInvalid in StoriesController#index
SQLite3::SQLException: no such column: votes_count: SELECT * FROM stories WHERE (votes_count >= 1) ORDER BY id DESC
Could anyone give me some pointers as to where I might be going wrong? Obviously the column hasn’t been created by my AddCounterCacheToStories migration file but I don’t know why.
When I run db:migrate AddCounterCacheToStories (which I think would only migrate this file - sorry newbie!) then I get the error "rake aborted! Don’t know how to build task ‘AddCounterCacheToStories’.
The code in the file is;
class AddCounterCacheToStories < ActiveRecord::Migration
def self.up
add_column :stories, :votes_count, :integer, :default => 0
Story.find(:all).each do |s|
s.update_attribute :votes_count, s.votes.length
end
end
def self.down
remove_column :stories, :votes_count
end
end