Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Active Record find(:all, :order => ) issue
    text
    copied!<p>I seem to be unable to use the ActiveRecord::Base.find option :order for more than one column at a time. </p> <p>For example, I have a "Show" model with date and attending columns.</p> <p>If I run the following code:</p> <pre><code>@shows = Show.find(:all, :order =&gt; "date") </code></pre> <p>I get the following results:</p> <pre><code>[#&lt;Show id: 7, date: "2009-04-18", attending: 2&gt;, #&lt;Show id: 1, date: "2009-04-18", attending: 78&gt;, #&lt;Show id: 2, date: "2009-04-19", attending: 91&gt;, #&lt;Show id: 3, date: "2009-04-20", attending: 16&gt;, #&lt;Show id: 4, date: "2009-04-21", attending: 136&gt;] </code></pre> <p>If I run the following code: </p> <pre><code>@shows = Show.find(:all, :order =&gt; "attending DESC") [#&lt;Show id: 4, date: "2009-04-21", attending: 136&gt;, #&lt;Show id: 2, date: "2009-04-19", attending: 91&gt;, #&lt;Show id: 1, date: "2009-04-18", attending: 78&gt;, #&lt;Show id: 3, date: "2009-04-20", attending: 16&gt;, #&lt;Show id: 7, date: "2009-04-18", attending: 2&gt;] </code></pre> <p>But, if I run:</p> <pre><code>@shows = Show.find(:all, :order =&gt; "date, attending DESC") </code></pre> <p>OR </p> <pre><code>@shows = Show.find(:all, :order =&gt; "date, attending ASC") </code></pre> <p>OR </p> <pre><code>@shows = Show.find(:all, :order =&gt; "date ASC, attending DESC") </code></pre> <p>I get the same results as only sorting by date:</p> <pre><code> [#&lt;Show id: 7, date: "2009-04-18", attending: 2&gt;, #&lt;Show id: 1, date: "2009-04-18", attending: 78&gt;, #&lt;Show id: 2, date: "2009-04-19", attending: 91&gt;, #&lt;Show id: 3, date: "2009-04-20", attending: 16&gt;, #&lt;Show id: 4, date: "2009-04-21", attending: 136&gt;] </code></pre> <p>Where as, I want to get these results:</p> <pre><code>[#&lt;Show id: 1, date: "2009-04-18", attending: 78&gt;, #&lt;Show id: 7, date: "2009-04-18", attending: 2&gt;, #&lt;Show id: 2, date: "2009-04-19", attending: 91&gt;, #&lt;Show id: 3, date: "2009-04-20", attending: 16&gt;, #&lt;Show id: 4, date: "2009-04-21", attending: 136&gt;] </code></pre> <p>This is the query being generated from the logs:</p> <pre><code>[4;35;1mUser Load (0.6ms)[0m [0mSELECT * FROM "users" WHERE ("users"."id" = 1) LIMIT 1[0m [4;36;1mShow Load (3.0ms)[0m [0;1mSELECT * FROM "shows" ORDER BY date ASC, attending DESC[0m [4;35;1mUser Load (0.6ms)[0m [0mSELECT * FROM "users" WHERE ("users"."id" = 1) [0m </code></pre> <p>Finally, here is my model:</p> <pre><code> create_table "shows", :force =&gt; true do |t| t.string "headliner" t.string "openers" t.string "venue" t.date "date" t.text "description" t.datetime "created_at" t.datetime "updated_at" t.decimal "price" t.time "showtime" t.integer "attending", :default =&gt; 0 t.string "time" end </code></pre> <p>What am I missing? What am I doing wrong?</p> <p><strong>UPDATE: Thanks for all your help, but it seems that all of you were stumped as much as I was. What solved the problem was actually switching databases. I switched from the default sqlite3 to mysql.</strong> </p>
 

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