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

  • Home
  • SEARCH
  • 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 4020016
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:10:33+00:00 2026-05-20T10:10:33+00:00

Moin, i stumbled upon a inconsistency in ActiveRecord. I tried to get all used

  • 0

Moin,

i stumbled upon a inconsistency in ActiveRecord.
I tried to get all used combinations of values in two columns of a large table.
First idea:

SELECT DISTINCT col1, col2 FROM table

Imagine a rails app that organizes meals as a model and each Meal has_many :noodles
Each Noodle has the attributes (and hence the DB table columns) color and shape.
My goal is to get the number of all present combinations of color and shape for a single meal.

Since AR does not provide a “distinct” method i used

my_meal.noodles.select("distinct color, shape")

and got (in the rails console stdout) a six line output of 8 Noodle objects (respectively their String representations). But:

>> my_meal.noodles.select("distinct color, shape").count
=> 1606

In fact my_meal contains 1606 noodles.
If i convert the the Relation to an array and get the size of it or use .all.count the result is correct.

So my question is, why does AR output 8 Objects but count all the DB lines?

A similar problem seems to be mentioned here but no answer is given.

Thanks and best regards,
Tim

  • 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-05-20T10:10:34+00:00Added an answer on May 20, 2026 at 10:10 am

    Okay, thanks to tadman for pushing me in the right direction.

    I digged somewhat deeper (especially in the log files) and what i found is a little bit weird.

    The problem was caused by the number of selected columns. If one selects only one column and counts the result

    my_meal.noodles.select("distinct color").count
    

    ActiveRecord creates the following SQL statement:

    SELECT COUNT(distinct color) AS count_id FROM "NOODLES" WHERE ("NOODLES".meal_id = 295)
    

    In case one selects two or more columns and applies count to it

    my_meal.noodles.select("distinct color, shape").count
    

    ActiveRecord forgets about that select clause and creates:

    SELECT COUNT(*) AS count_id FROM "NOODLES" WHERE ("NOODLES".meal_id = 295)
    

    This may be right, since (SQL’s) COUNT allows only one or less columns as parameters. Add a group before the count and anything is fine:

    my_meal.noodles.select("distinct color, shape").group("color, shape").count
    
    SELECT COUNT(*) AS count_all, color, shape AS color_shape FROM "NOODLES" WHERE ("NOODLES".meal_id = 295) GROUP BY color, shape
    

    Apart from this AS color_shape it is exact what i expected. BUT… only it returns this:

    >> my_meal.noodles.select("distinct color, shape").group("color, shape").count
    => {star=>309, circle=>111, spaghetti=>189, square=>194, triangle=>179, bowtie=>301, shell=>93, letter=>230}
    
    >> my_meal.noodles.select("distinct color, shape").group("color, shape").count.class
    => ActiveSupport::OrderedHash
    

    This weird return value is (apart from order which depends on the DB) identical with the result and return value of

    my_meal.noodles.group("shape").count
    

    Conclusion:
    As pointed out here there is still a gap between relations (may they be mathematical or arel relations) and ActiveRecord::Relations.
    I can see the advantages of pressing the result in the patterns of a model as often as possible (at least in the context of a Rails app).
    However real relations are not the result of the combination of several operations but the result of the concatenation of those operations.
    In general the chainability of ActiveRecord::Relations is a great thing but there are some design decisions i cannot follow.
    If you can’t depend on the assurance that each action returns a new relation to work with, it looses much of its inuitional appeal.

    As for the solution of my problem, i will use the above-mentioned group solution and some kind of dirty workaround for the count operation:

    my_meal.noodles.select("distinct color, shape").group("color, shape").all.count
    

    This compresses the results to an acceptable minimum before pulling them out of the database and creating expensive objects just to count them. Alternatively one could use a handwritten SQL query, but why have a Rails and do not use it, huh? 😉

    Thanks for your help,
    Tim

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

Sidebar

Related Questions

I have inserted a row into my table, and I want to get it's
Moin! using (ZipFile zip = new ZipFile()) { // add this map file into
Moin! I have a situation where I try to display an error message using
moin-moin, I know, I am the only living person, still writing software for OS/2,
There are several utilities — all with different procedures, limitations, and target operating systems
I use in my iPhone application a very big database. An index is used
All, I've got the following code to display my maps: <script type=text/javascript src=http://maps.google.com/maps/api/js?sensor=false></script> <script
All, I've got the following code to display my markers on my maps: <script
I've used Python's miniDom library before. It had a useful method that I'm missing
All, I've got the following code: <script type=text/javascript> function Store( lat, long, text )

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.