Need help understanding :order usage

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.

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”

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/

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