Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, thanks to <a href="https://stackoverflow.com/users/87189/tadman">tadman</a> for pushing me in the right direction.</p> <p>I digged somewhat deeper (especially in the log files) and what i found is a little bit weird.</p> <p>The problem was caused by the number of selected columns. If one selects only one column and counts the result</p> <pre><code>my_meal.noodles.select("distinct color").count </code></pre> <p>ActiveRecord creates the following SQL statement:</p> <pre><code>SELECT COUNT(distinct color) AS count_id FROM "NOODLES" WHERE ("NOODLES".meal_id = 295) </code></pre> <p>In case one selects two or more columns and applies <code>count</code> to it</p> <pre><code>my_meal.noodles.select("distinct color, shape").count </code></pre> <p>ActiveRecord forgets about that select clause and creates:</p> <pre><code>SELECT COUNT(*) AS count_id FROM "NOODLES" WHERE ("NOODLES".meal_id = 295) </code></pre> <p>This may be right, since (SQL's) <code>COUNT</code> allows only one or less columns as parameters. Add a <code>group</code> before the <code>count</code> and anything is fine:</p> <pre><code>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 </code></pre> <p>Apart from this <code>AS color_shape</code> it is exact what i expected. BUT... only it returns this:</p> <pre><code>&gt;&gt; my_meal.noodles.select("distinct color, shape").group("color, shape").count =&gt; {star=&gt;309, circle=&gt;111, spaghetti=&gt;189, square=&gt;194, triangle=&gt;179, bowtie=&gt;301, shell=&gt;93, letter=&gt;230} &gt;&gt; my_meal.noodles.select("distinct color, shape").group("color, shape").count.class =&gt; ActiveSupport::OrderedHash </code></pre> <p>This weird return value is (apart from order which depends on the DB) identical with the result and return value of</p> <pre><code>my_meal.noodles.group("shape").count </code></pre> <p>Conclusion:<br> As <a href="http://metautonomo.us/2010/05/11/activerecord-relation-vs-arel/" rel="nofollow noreferrer">pointed out here</a> there is still a gap between relations (may they be mathematical or arel relations) and ActiveRecord::Relations.<br> 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).<br> However real relations are not the result of the <em>combination</em> of several operations but the result of the <em>concatenation</em> of those operations. In general the chainability of ActiveRecord::Relations is a great thing but there are some design decisions i cannot follow.<br> 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.</p> <p>As for the solution of my problem, i will use the above-mentioned <code>group</code> solution and some kind of dirty workaround for the count operation:</p> <pre><code>my_meal.noodles.select("distinct color, shape").group("color, shape").all.count </code></pre> <p>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? ;-)</p> <p>Thanks for your help,<br> Tim</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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