【问题标题】:Post Sorting By Vote Database按投票数据库排序
【发布时间】:2015-12-04 11:03:58
【问题描述】:

我有一个页面,用户可以在其中添加收藏夹。现在页面将他们的帖子分类到收藏夹中,但在 Post 数据库 created_at 字段中按 .order(created_at: :desc) 对它们进行排序。如何更改它以使用 Vote 表及其 created_at frield。

@posts = Post.where(id: current_user.find_voted_items.map(&:id)).order(created_at: :desc).paginate(:page => params[:page], :per_page => 36)

这里是相关的架构

create_table "votes", force: :cascade do |t|
  t.integer  "votable_id"
  t.string   "votable_type"
  t.integer  "voter_id"
  t.string   "voter_type"
  t.boolean  "vote_flag"
  t.string   "vote_scope"
  t.integer  "vote_weight"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "posts", force: :cascade do |t|
  t.string   "soundcloud"
  t.string   "title"
  t.string   "artist"
  t.string   "audio"
  t.string   "image"
  t.datetime "created_at",                            null: false
  t.datetime "updated_at",                            null: false
  t.string   "download"
  t.integer  "cached_votes_total",      default: 0
  t.integer  "cached_votes_score",      default: 0
  t.integer  "cached_votes_up",         default: 0
  t.integer  "cached_votes_down",       default: 0
  t.integer  "cached_weighted_score",   default: 0
  t.integer  "cached_weighted_total",   default: 0
  t.float    "cached_weighted_average", default: 0.0
end

谢谢

【问题讨论】:

    标签: ruby-on-rails database acts-as-votable


    【解决方案1】:

    如果你想返回帖子,使用帖子数据库更有意义。如果您想使用投票数据库返回一个帖子的所有投票,您可以尝试这样的操作:
    @votes = Vote.where(votable_id: some_post.id).order('created_at DESC')

    但我不确定我是否理解您希望从投票数据库返回的内容。

    【讨论】:

      猜你喜欢
      • 2012-10-29
      • 1970-01-01
      • 2014-08-16
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多