Note that there are some explanatory texts on larger screens.

plurals
  1. PORails scope issue
    text
    copied!<p>I am trying to add a scope to model. I am looking to calculate total plays from the db. I am on Rails 3.2.13</p> <p>MYSQL query provides the expected results. </p> <pre><code>SELECT COUNT(DISTINCT(CONCAT(stats.filename,stats.viewer_id))) FROM `stats` WHERE `stats`.`user_id` = 4 AND (stats.cf_status in ('Hit', 'Miss', 'RefreshHit', 'Error')) AND (date_time between '2013-04-14 05:00:00' and '2013-04-21 16:35:16') </code></pre> <p>= 169 plays</p> <p>I am trying to get this scope to work but Rails is ignoring it when I check the logs.</p> <p><strong>Stats model</strong></p> <pre><code>scope :only_valid, -&gt; { where("stats.cf_status in ('Hit', 'Miss', 'RefreshHit', 'Error')") } scope :totviews, -&gt; { where(" COUNT(DISTINCT(CONCAT(stats.filename,stats.viewer_id)))") } </code></pre> <p><strong>Dashboard Controller</strong> total_views: Stat.for_user(current_user).only_valid.between(@start_at,@end_at).totviews.count,</p> <p><strong>Output from the logs</strong> Still shows the original query not the modified one.</p> <pre><code>SELECT COUNT(*) FROM `stats` WHERE `stats`.`user_id` = 4 AND (stats.cf_status in ('Hit', 'Miss', 'RefreshHit', 'Error')) AND (date_time between '2013-04-14 05:00:00' and '2013-04-21 17:17:29') </code></pre> <p>= 4589 plays</p> <p>Any help is appreciated. Thanks</p> <pre><code>expose(:overview_chart_views) { { total_views: to_col(Stat.for_user(current_user).only_valid.between(@start_at, @end_at).by_day), total_viewers: to_col(Stat.unique_plays.for_user(current_user).only_valid.between(@start_at, @end_at).by_day), new_viewers: to_col(Viewer.for_user(current_user).with_first_views.only_valid.between(@start_at, @end_at).by_day), return_viewers: to_col(Viewer.for_user(current_user).without_first_views.only_valid.between(@start_at, @end_at).by_day) } private def to_col(results) lookup = {} results.each {|row| if !row['date'].kind_of?(Date) row['date'] = Date.parse(row['date']); end lookup[row['date']] = row['result'].to_i } (@start_at.to_date..@end_at.to_date).to_a.map {|date| lookup[date] || 0} end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload