Sorting In rails ..

I am executing an sql query .. as like .. abcarray = Abc.find(:all, :conditions => { :id => 'whatever', :name => "Hemant" } )

abcarray records are in this format:- id: 1, created_at: nil, updated_at: nil, day: 1, clinic_id: 13, time: 530

abcarray consists of two fields day and time actually .. so i want to sort them using date first and then time second ..

Is dere any way to do it .. ? Waiting for u guyz ..

Hemant Bhargava wrote:

I am executing an sql query .. as like .. abcarray = Abc.find(:all, :conditions => { :id => 'whatever', :name => "Hemant" },

:order => 'date, time'   )

abcarray records are in this format:- id: 1, created_at: nil, updated_at: nil, day: 1, clinic_id: 13, time: 530

abcarray consists of two fields day and time actually .. so i want to sort them using date first and then time second ..

Is dere any way to do it .. ?

Now the lecture. Using ActiveRecord requires knowing SQL, even if AR does a very good job of hiding its verbosity. At the simplest extreme, AR find() often allows you to write the same kind of command, such as :group or :order, in the same way as the matching SQL GROUP BY or ORDER BY clause.

Next, a :datetime field holds both the date and time together, so you ought to edit your model's migration and run rake db:migrate:reset to erase all your data and put those two fields together!

I had done it .. using abcarray = Abc.find(:all, :conditions => { :id => 'whatever', :name => "Hemant" }, :order => 'day' ) and then .. abcarray.sort {|a,b| a.time <=> b.time}

I think this is the right way to do it .. ? What say champs ..? :slight_smile:

Phlip wrote:

Hemant if you do not own this book, you should consider purchasing it:

It is a _must_ have for anyone new to Rails. The Ruby book from PragProg is also pretty good as a reference guide to the language as a whole.

Ok .. Thanks .. I have this book ..

Zach Karpinski wrote:

The above method:-

:order => 'day, time_start, time_end' is returning me records only in day sorted.. This line is not sorting in context of time_start and time_end ..

:cry:

Hemant Bhargava wrote:

Have a look in the rails log (in your application/log) and see what sql query is being used, this may give you a clue. If it looks correct check what type the time columns are.

Colin