N.Smith N.Smith - 1 year ago 71
Ruby Question

Getting error when trying to order by up votes (act_as_votable gem)

So I have the voting working perfectly fine. But when I try to order my posts by most up votes I get this error saying statement invalid in Post Index.

Error message

This is my post controller

def index
@posts = Post.all.order(:cached_votes_up => :desc)

I have added the Cached votes to posts table migration as well here is the first half of it

class AddCachedVotesToPosts < ActiveRecord::Migration
def self.up
add_column :posts, :cached_votes_total, :integer, :default => 0
add_column :posts, :cached_votes_score, :integer, :default => 0
add_column :posts, :cached_votes_up, :integer, :default => 0
add_column :posts, :cached_votes_down, :integer, :default => 0
add_column :posts, :cached_weighted_score, :integer, :default => 0
add_column :posts, :cached_weighted_total, :integer, :default => 0
add_column :posts, :cached_weighted_average, :float, :default => 0.0
add_index :posts, :cached_votes_total
add_index :posts, :cached_votes_score
add_index :posts, :cached_votes_up
add_index :posts, :cached_votes_down
add_index :posts, :cached_weighted_score
add_index :posts, :cached_weighted_total
add_index :posts, :cached_weighted_average

If anymore code will help I will gladly post it

Posts table

create_table "posts", force: :cascade do |t|
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"

Answer Source

cached_votes_up column not added to your database yet. Please run this command,

rake db:migrate:reset

It will reset your database migrations and all new columns will be added. It should solve your problem.

note: all your database records will be removed by using this command.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download