尝试使用acts_as_votable返回最受欢迎的博客文章

时间:2018-09-24 20:21:17

标签: ruby-on-rails activerecord model acts-as-votable

这是我的模型:

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

视图:

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

控制器:

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

我收到一条错误消息,指出ActiveRecord关系的未定义方法“ to_model”

我在做什么错?

0 个答案:

没有答案