【发布时间】:2013-12-24 01:04:13
【问题描述】:
如何将此 SQL 转换为 Rails?
select tags.tag, count(*) c
from taggings, tags
where taggings.post_id = 1 and taggings.tag_id = tags.id
group by tag
order by c desc;
我尝试使用
Post.first.tags.select(:tag, "count(*) as count").group(:tag).order(count: :desc)
但它给出了一个错误
ArgumentError:参数数量错误(0..1 为 2)
型号
class Post < ActiveRecord::Base
has_many :taggings, :dependent => :destroy
has_many :tags, :through => :taggings
class Tagging < ActiveRecord::Base
belongs_to :post
belongs_to :user
belongs_to :tag
【问题讨论】:
标签: sql ruby-on-rails ruby-on-rails-4 rails-activerecord