【问题标题】:Rails Active Record count associationsRails Active Record 计数关联
【发布时间】:2017-10-14 20:31:47
【问题描述】:

我一直在尝试所有可以在 Rails 指南和此处找到的方法,按两个模型之间的关联数进行计数和排序。

控制器中的查询:

  def top_mires
    @title = "Top Posts"
    @posts = Post.joins('INNER JOIN favorites ON posts.id = favorites.favorited_id').select('posts.*, COUNT(favorites.favorited_id) AS favorite_count').group('posts.id').order('favorite_count desc').limit(10)
    render 'favorite_posts/show'
  end

模型:

帖子:

class Post < ApplicationRecord
  include PublicActivity::Common

  acts_as_taggable

  belongs_to :user
  has_many :comments
  has_many :favorites, as: :favorited

  default_scope -> { order(created_at: :desc) }

  mount_uploader :picture, PictureUploader

  validates :user_id, presence: true
  validates :post_type, presence: true
  validates :content, presence: true

最喜欢的:

class Favorite < ApplicationRecord
  include PublicActivity::Model
  tracked only: [:create], owner: :user, recipient: :favorited

  belongs_to :favorited, polymorphic: true, counter_cache: :favorite_count
  belongs_to :user
end

但我得到了这个错误:

SQLite3::SQLException: no such column: favorite_count: SELECT  COUNT(*) AS count_all, posts.id AS posts_id FROM "posts" INNER JOIN favorites ON posts.id = favorites.favorited_id GROUP BY posts.id ORDER BY "posts"."created_at" DESC, favorite_count desc LIMIT ?

Favorites.count 怎么不是列?我也多次使用 posts.favorites.count 来显示 ERB 中帖子的收藏计数,但它在这里不起作用......

谢谢你,

【问题讨论】:

    标签: sql ruby-on-rails ruby sqlite activerecord


    【解决方案1】:

    此错误表示 SQLite 不能在 order by 块中使用 favorite_count 列。如果您以显式方式重写order by favorite_count,它将起作用:

    Post.
      joins('INNER JOIN favorites ON posts.id = favorites.favorited_id').
      select('posts.*, COUNT(favorites.favorited_id) AS favorite_count').
      group('posts.id').
      order('COUNT(favorites.favorited_id) desc').limit(10)
    

    【讨论】:

    • 感谢您的帮助,但这是发布的 SQL(为简单起见,我在此处仅使用“posts.id”):
      SELECT posts.id, COUNT(favorites.favorited_id) FROM "posts" INNER JOIN favorites ON posts.id = favorites.favorited_id GROUP BY posts.id ORDER BY COUNT(favorites.favorited_id) desc LIMIT ? [["LIMIT", 10]]
      结果是:
      =&gt; #&lt;ActiveRecord::Relation [#&lt;Post id: 206&gt;, #&lt;Post id: 293&gt;, #&lt;Post id: 296&gt;, #&lt;Post id: 322&gt;, #&lt;Post id: 331&gt;, &amp;c为什么还是按“post.created_at”排序?为什么收藏夹仍然没有计数?
    • 默认情况下,ruby 的打印不会将自定义列显示为favorite_count。试试posts = Post.joins('INNER JOIN favorites ON posts.id = favorites.favorited_id').select('posts.*, COUNT(favorites.favorited_id) AS favorite_count').group('posts.id').order('COUNT(favorites.favorited_id) desc').limit(10); posts.each { |p| puts "id: #{p.id}, favorite_count: #{p.favorite_count}, created_at: #{p.created_at}" }。可能是created_at order 和 count order 对于您的数据是相同的。
    • 我需要在 Post 模型中添加一列“favorites_count”吗?
    • 您不需要将favorites_count 添加到 Post 模型。如果列在 sql 查询中,在您的情况下为 favorites_count,Rails 允许在结果记录对象处调用 favorites_count
    • 我继续并使用以下内容添加了列:ryan.mcgeary.org/2016/02/05/…,它工作得非常好,我可以调用:Post.select('posts.id, posts.favorites_count').order('posts.favorites_count),但它仍然呈现ORDER BY "posts"."created_at" DESC, posts.favorites_count。如何覆盖默认订单范围?
    【解决方案2】:

    我继续使用以下内容添加了专栏:ryan.mcgeary.org/2016/02/05/... 效果非常好,我可以致电:Post.unscoped.order('posts.favorites_count desc').limit(10)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多