1

So here's my model:

home_blog.rb

def self.highest_voted
     self.order("cached_votes_score DESC")
end

schema.rb

  create_table "home_blogs", force: :cascade do |t|
    t.string "name"
    t.text "entry"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "image"
    t.integer "cached_votes_score", default: 0
    t.integer "cached_votes_total", default: 0
    t.integer "cached_votes_up", default: 0
    t.integer "cached_votes_down", default: 0
    t.index ["cached_votes_down"], name: "index_home_blogs_on_cached_votes_down"
    t.index ["cached_votes_score"], name: "index_home_blogs_on_cached_votes_score"
    t.index ["cached_votes_total"], name: "index_home_blogs_on_cached_votes_total"
    t.index ["cached_votes_up"], name: "index_home_blogs_on_cached_votes_up"
  end

the view:

<div>
          <%= link_to "Most Popular", @highest_voted %>
        </div>

the controller:

def index
    @home_blogs = HomeBlog.order('created_at DESC').paginate(:page => params[:page], :per_page => 3)
    @highest_voted = HomeBlog.highest_voted.limit(1)
  end

I get an error that says undefined method `to_model' for activerecord relation

What am I doing wrong?

dmberko11
  • 427
  • 7
  • 17
  • you should use `HomeBlog.highest_voted.first` (`limit(1)` still return an ActiveRecord::Relation with max 1 element inside) – MrYoshiji Sep 24 '18 at 20:31
  • @MrYoshiji Thanks! That got me on the right track. I actually changed my method a little in the model to rank highest_voted based on the vote count and called HomeBlog.highest_voted.first in the controller and that seems to do the job – dmberko11 Sep 24 '18 at 22:44

0 Answers0