如何将SQL查询编写为命名范围?
如何将以下SQL查询转换为命名的\u范围如何将SQL查询编写为命名范围?,sql,ruby-on-rails,Sql,Ruby On Rails,如何将以下SQL查询转换为命名的\u范围 select users.*, sum(total_quantity * total_price) as points_spent from orders join users on users.id = orders.user_id where pay_type = 'points' group by user_id order by points_spent desc 谢谢 试试这个 class User < ActiveRecor
select users.*, sum(total_quantity * total_price) as points_spent
from orders
join users on users.id = orders.user_id
where pay_type = 'points'
group by user_id
order by points_spent desc
谢谢 试试这个
class User < ActiveRecord::Base
named_scope :your_name,
:select=>" users.*,sum(total_quantity * total_price) as points_spent",
:joins => :orders,
:conditions => ['pay_type = ?', 'points'],
:group ="user_id ",
:order=>'points_spent desc'
end
class用户“users.*,sum(总数量*总价格)作为花费的点数”,
:joins=>:订单,
:条件=>['pay_type=?','points'],
:group=“user\u id”,
:order=>“点数”
结束
试试这个
class User < ActiveRecord::Base
named_scope :your_name,
:select=>" users.*,sum(total_quantity * total_price) as points_spent",
:joins => :orders,
:conditions => ['pay_type = ?', 'points'],
:group ="user_id ",
:order=>'points_spent desc'
end
class用户“users.*,sum(总数量*总价格)作为花费的点数”,
:joins=>:订单,
:条件=>['pay_type=?','points'],
:group=“user\u id”,
:order=>“点数”
结束