Sorting

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…

        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:

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

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…

        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:

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.

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 [email protected] 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/.


Ed Hickey
Developer
Litmus Media
816-533-0409
[email protected]
A Member of Think Partnership, Inc
www.ThinkPartnership.com
Amex ticker symbol: THK

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

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…

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