Mysql 从Rails 4.1的`关系#计数'中删除不推荐的`:distinct`选项
代码: SQL查询Mysql 从Rails 4.1的`关系#计数'中删除不推荐的`:distinct`选项,mysql,ruby-on-rails,oracle,ruby-on-rails-4,Mysql,Ruby On Rails,Oracle,Ruby On Rails 4,代码: SQL查询 checkin_scope.count(:select => "user_id", :distinct => true) 我需要删除:distinct=>true,因为它已在Rails 4.1中删除 好的,我可以解决这个问题 SELECT COUNT(DISTINCT `checkins`.`user_id`) AS count_user_id, location_id AS location_id FROM `checkins` INNE
checkin_scope.count(:select => "user_id", :distinct => true)
我需要删除:distinct=>true,因为它已在Rails 4.1中删除 好的,我可以解决这个问题
SELECT COUNT(DISTINCT `checkins`.`user_id`) AS count_user_id,
location_id AS location_id
FROM `checkins` INNER JOIN `locations` ON `locations`.`id` = `checkins`.`location_id`
WHERE `checkins`.`business_id` = 452 AND `checkins`.`status` = 'loyalty'
AND `locations`.`status` = 'approved' AND
`checkins`.`location_id` IN (302825, 302838, 302839, 302901)
AND (date(checkins.created_at) between '2014-10-11' and '2014-11-11')
GROUP BY location_id
其中checkin_scope是一种方法
checkin_scope.count("DISTINCT(checkins.user_id)")
如果现在移除它会发生什么?只是curious@CaffeineCoder删除它不会产生不同的输出。将unique选项移动到一个作用域块中,在这里阅读更多关于它的内容。我想您已经读到,在Rails 4中,他们删除了deprecated:distinct option from Relation#count。您是否尝试过在指定关系的地方使用uniq?
def checkin_scope
cscope = @business.checkins.loyalty_punchh.joins(:location).where(:locations => {:status => "approved"}).group(:location_id)
cscope = cscope.for_location(@location) if @location
if @from && @to
cscope = cscope.where("date(checkins.created_at) between ? and ?",@from.to_date,@to.to_date)
end
cscope
end