Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9071103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:50:01+00:00 2026-06-16T17:50:01+00:00

I can’t figure out how to count objects in a scope appropriately. For this

  • 0

I can’t figure out how to count objects in a scope appropriately.

For this example, let’s assume products (“skus”) had a lat and lng, and so do users.

I have a find_by_sql example that works:

  def self.with_max_distance(user, distance)
    Sku.find_by_sql(
    <<-eos
      SELECT *,
      ( 3959 * acos( cos( radians(#{user.lat}) ) * cos( radians( skus.lat ) )
      * cos( radians(skus.lng) - radians(#{user.lng})) + sin(radians(#{user.lat}))
      * sin( radians(skus.lat)))) AS distance
      FROM skus
      HAVING distance < #{distance}
    eos
    )
  end

I was able to get the following scope to work partially:

  scope :scope_with_max_distance, lambda { |user, distance|
    { :select => "skus.*, ( 3959 * acos( cos( radians(#{user.lat}) ) * cos( radians( skus.lat ) ) * cos( radians(skus.lng) - radians(#{user.lng})) + sin(radians(#{user.lat})) * sin( radians(skus.lat)))) AS distance",
      :having => "distance < #{distance}"
    }
  }

The scope appears to get the objects correctly, and although I haven’t tested it extensively, it appears to work if I chain it with my other scopes.

However, I am unable to count() the objects, which is really annoying. I know I can use length(), but the fact that it doesn’t work with count leaves me wondering if there’s a better way to do this.

1.9.3p125 :048 > Sku.scope_with_max_distance(User.first, 50).limit(10).count
  User Load (0.5ms)  SELECT `users`.* FROM `users` LIMIT 1
   (0.6ms)  SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM `skus` HAVING distance < 50 LIMIT 10) subquery_for_count 
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'distance' in 'having clause': SELECT COUNT(count_column) FROM (SELECT  1 AS count_column FROM `skus`  HAVING distance < 50 LIMIT 10) subquery_for_count 

I’d really appreciate any advice or ideas – I’ve tried many different approaches but haven’t been able to find one that works completely. Please let me know if the issue needs more clarification.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:50:03+00:00Added an answer on June 16, 2026 at 5:50 pm

    count will replace your select with just the simple elements it needs to build a count as efficiently as it can. It will not check other constraints to ensure that the SQL is valid. You can replace the distance alias in having with the formula you would be selecting to get around this problem. Also, I recommend that you replace having with where to avoid implicit grouping that is not database portable:

    scope :scope_with_max_distance, lambda { |user, distance|
      distance_formula = "(3959 * acos( cos( radians(#{user.lat}) ) * cos( radians( skus.lat ) ) * cos( radians(skus.lng) - radians(#{user.lng})) + sin(radians(#{user.lat})) * sin( radians(skus.lat))))"
      { :select => "skus.*, #{distance_formula} AS distance",
        :where => "#{distance_formula} < #{distance}"
      }
    }
    

    One last thing, you are calling count after limit(10) which would mean that count will never be greater than 10. That’s not inherently wrong, but I’m not sure that it’s what you meant to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can any one help me in sorting this out in sed/awk/perl Input file Start
Can anyone help me trying to find out why this doesn't work. The brushes
Can anyone let me know how can we change the value of kendo combobox
Can anyone explain to me why this program: for(float i = -1; i <
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can someone maybe tell me why this is not working? I have used echo
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
can someone explain what means this line, or provide with source where it is
Can someone please explain how this code is working for me? I don't understand
Can I write this with less code, also is .click(function preferred for mobile or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.