# Need help understanding :order usage

**URL:** <https://www.ruby-forum.com/t/need-help-understanding-order-usage/187091>\
**Category:** Rails\
**Created:** [April 16, 2010, 7:46pm UTC](https://www.ruby-forum.com/t/need-help-understanding-order-usage/187091 "2010-04-16T19:46:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [April 16, 2010, 7:46pm UTC](https://www.ruby-forum.com/t/need-help-understanding-order-usage/187091/1 "2010-04-16T19:46:52Z")

</div>

Import has\_many IRows  
IRow belongs to Import

@import = Import.find(params[:id])

Shouldn’t both of the following statements work the same?

#1 :@i\_rows = @import.i\_rows(:order =\> :row\_sort)  
Generates: SELECT \* FROM “i\_rows” WHERE (“i\_rows”.import\_id =  
4)

#2 @i\_rows = IRow.find\_all\_by\_import\_id(@import.id, :order  
=\> :row\_sort)  
Generates: SELECT \* FROM “i\_rows” WHERE (“i\_rows”.“import\_id” =  
4) ORDER BY row\_sort

What is wrong with #1. It returns same data as #2, but just not  
sorted. No syntax or SQL errors are generated by #1.

---

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [April 16, 2010, 11:04pm UTC](https://www.ruby-forum.com/t/need-help-understanding-order-usage/187091/2 "2010-04-16T23:04:46Z")

</div>

RVRoadie wrote:

> Import has\_many IRows  
> IRow belongs to Import
> 
> @import = Import.find(params[:id])
> 
> Shouldn’t both of the following statements work the same?
> 
> #1 :@i\_rows = @import.i\_rows(:order =\> :row\_sort)  
> Generates: SELECT \* FROM “i\_rows” WHERE (“i\_rows”.import\_id =  
> 4)
> 
> #2 @i\_rows = IRow.find\_all\_by\_import\_id(@import.id, :order  
> =\> :row\_sort)  
> Generates: SELECT \* FROM “i\_rows” WHERE (“i\_rows”.“import\_id” =  
> 4) ORDER BY row\_sort
> 
> What is wrong with #1. It returns same data as #2, but just not  
> sorted. No syntax or SQL errors are generated by #1.

The :order is an option passed to the “find” method of an ActiveRecord  
instance, but @import.i\_rows (assuming this is a to\_many association) is  
a reference to an Array of ActiveRecord instances so #1 makes no sense.

I think you might want @i\_rows = @import.i\_rows.find(:all, :order =\>  
“row\_sort”)

Besides that you’re syntax for setting :order is rather non-idomatic  
Rails. Did you look at the documentation for usage of :order? Using the  
symbol will work, but symbols should be used to identify things not  
represent SQL fragments. The text of the symbol in your case is  
important as a string of characters rather than the name of some  
“thing.” Use a string for those cases.

Excerpt from Rails docs:  
:order - An SQL fragment like â€œcreated\_at DESC, nameâ€

---

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [April 17, 2010, 4:41am UTC](https://www.ruby-forum.com/t/need-help-understanding-order-usage/187091/3 "2010-04-17T04:41:18Z")

</div>

Thanks for taking the time to set me straight.

I confess that I did not look at the API. I have a hard time  
navigating the API and sometimes avoid it when I really should be  
looking there first.

As a result of your comment, I did find this link:  
[http://railsapi.com/doc/rails-v2.3.5/](http://railsapi.com/doc/rails-v2.3.5/)

It is a searchable Rails API. It is much easier (for me) to use.
