SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simply Rails 2 - Counter Cache - please help!

    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;
    Code Ruby:
    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
    Last edited by Mittineague; Jan 15, 2011 at 15:25. Reason: reformatting bbcode tags

  2. #2
    Once I was a Factory Worker goofy's Avatar
    Join Date
    Dec 2004
    Location
    Sydney Australia
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check your schema.rb file to see if your data structure is right. Sounds like the migration is failing.
    Goofy
    Life is what you make it!
    Follow your dreams!

  3. #3
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the late reply!

    Yes the column isn't in the schema.rb because the migration fails. I've gone through the previous chapter of the book again and I get the same error on the Counter Cache section!

  4. #4
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "When I run db:migrate AddCounterCacheToStories"

    You should just be able to run "rake db:migrate", no? At least that's how it's done in rails 3..

    You shouldn't need to specify the name of the migration I don't think


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •