# Sorting

**URL:** <https://www.ruby-forum.com/t/sorting/81877>\
**Category:** Rails\
**Created:** [January 15, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/sorting/81877 "2007-01-15T23:07:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 15, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/sorting/81877/1 "2007-01-15T23:07:04Z")

</div>

Hello, i am very new to RoR and last 2 weeks i ve been reading a lot  
about them…

I have manage to create a quick application, but i am having problems  
figuring out how to sort the fields…

```auto
        def list
                @running = Boards.find_all("running = 1")
                @waiting = Boards.find_all("waiting = 1")
                @on_hold = Boards.find_all("running = 0", "waiting = 0")
        end
```

Thats what i have as the list def and it works but it order is as first  
in… I tried the following:

```auto
def list
   @running = Boards.find(:all, :conditions => "running = 1", :order =>
priority ASC, quantity DESC')
   @waiting = Boards.find(:all, :conditions => "waiting = 1", :order =>
priority ASC, quantity DESC'
   @on_hold = Boards.(:all, :conditions => "running = 0, waiting = 0",
:order => priority ASC, quantity DESC'
end
```

And it gives me errors… I need to sort them with ASC priotity and DESC  
quantity (priority and quantity are columns on my table)

Any ideas…?

Thanks

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 15, 2007, 11:23pm UTC](https://www.ruby-forum.com/t/sorting/81877/2 "2007-01-15T23:23:12Z")

</div>

Ro Vent wrote:

> Hello, i am very new to RoR and last 2 weeks i ve been reading a lot  
> about them…
> 
> I have manage to create a quick application, but i am having problems  
> figuring out how to sort the fields…
> 
> ```auto
> def list
> > @running = Boards.find_all("running = 1")
> > @waiting = Boards.find_all("waiting = 1")
> > @on_hold = Boards.find_all("running = 0", "waiting = 0")
> > end
> ```
> 
> Thats what i have as the list def and it works but it order is as first  
> in… I tried the following:
> 
> ```auto
> def list
> > @running = Boards.find(:all, :conditions => "running = 1", :order =>
> > priority ASC, quantity DESC')
> > @waiting = Boards.find(:all, :conditions => "waiting = 1", :order =>
> > priority ASC, quantity DESC'
> > @on_hold = Boards.(:all, :conditions => "running = 0, waiting = 0",
> > :order => priority ASC, quantity DESC'
> > end
> ```
> 
> And it gives me errors… I need to sort them with ASC priotity and DESC  
> quantity (priority and quantity are columns on my table)
> 
> Any ideas…?
> 
> Thanks

Place your :order in quotes:

@running = Boards.find(:all, :conditions =\> “running = 1”, :order =\>  
“priority ASC, quantity DESC”’)

Also, you may want to tail the development log to see the SQL being  
generated.

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 15, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/sorting/81877/3 "2007-01-15T23:32:28Z")

</div>

what’s the error?

it could be that Boards.find is including other associations so you  
might  
need to write something like  
@running = Boards.find(:all, :conditions =\> “running = 1”, :order =\> "  
boards.priority ASC, quantity DESC")

though looking at your pasted code, you have a single quote before the  
closing paren (which is missing on 2 of the lines) that was never  
opened…that could also be your issue.

ed

On 1/15/07, Ro Vent [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @running = Boards.find_all("running = 1")
> 
> ```
> 
> @running = Boards.find(:all, :conditions =\> “running = 1”, :order =\>
> 
> Any ideas…?
> 
> Thanks
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ed Hickey  
Developer  
Litmus Media  
816-533-0409  
removed\_email\_address@domain.invalid  
A Member of Think Partnership, Inc  
[www.ThinkPartnership.com](http://www.ThinkPartnership.com)  
Amex ticker symbol: THK

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 15, 2007, 11:34pm UTC](https://www.ruby-forum.com/t/sorting/81877/4 "2007-01-15T23:34:28Z")

</div>

Ro Vent wrote:

> ```
> @waiting = Boards.find_all("waiting = 1")
> 
> ```
> 
> priority ASC, quantity DESC’)  
> Any ideas…?
> 
> Thanks

It looks like you are missing quotes around your :order params.

Matthew M.  
[blog.mattmargolis.net](http://blog.mattmargolis.net)

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 16, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/sorting/81877/5 "2007-01-16T15:25:38Z")

</div>

Thanks i overlooked at that…

After i addded the quotes it worked, however that only evaluates one  
expresion:

@on\_hold = Boards.find(:all, :conditions =\> “running = 0, waiting = 0”,  
:order =\> “priority ASC, quantity DESC”)

That means that it only takes one condition after the :conditions =\>…  
Is there a simple way of taking 2 conditions…?

Also i have date fields and i was wondering how can i only display the  
month and day…? At this moment it displays 2007-01-19 and i want it  
to display 01-19…

Thanks…

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 16, 2007, 3:43pm UTC](https://www.ruby-forum.com/t/sorting/81877/6 "2007-01-16T15:43:10Z")

</div>

Ro Vent wrote:

> After i addded the quotes it worked, however that only evaluates one  
> expresion:
> 
> @on\_hold = Boards.find(:all, :conditions =\> “running = 0, waiting = 0”,  
> :order =\> “priority ASC, quantity DESC”)
> 
> That means that it only takes one condition after the :conditions =\>…  
> Is there a simple way of taking 2 conditions…?

The :conditions option is simply an SQL fragment that gets inserted into  
the SQL WHERE clause, so you can indeed only specify one “condition”.  
However, that condition can be anything that is valid in a WHERE clause  
… in your case, you would want it to be:

@on\_hold = Boards.find(:all, :conditions =\> “running = 0 AND waiting =  
0”, :order=\> “priority ASC, quantity DESC”)

As a side note, specifying the :conditions option as a single string is  
fine if you are completely specifying the conditions in your code.  
However, if any part of it is coming from user input, you will need to  
use the array form of the :conditions option to prevent against SQL  
injection. For example:

@employees = Employee.find(:all, :conditions =\> [“last\_name = ?”,  
params[:last\_name]])

See the documentation for the find method for details:  
[http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000860](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000860)
