如何将此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:错误的参数数量(2为0..1)
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
答案 0 :(得分:2)
尝试:
Posts.first.tags.select("tags.tag, count(1) as tag_count").group('tags.tag').order('tag_count desc')