Ishu
Ishu

Reputation: 638

How can i order the eager loaded model in rails

I shot a find query to include certain models in rails but i want to order the included models by my custom criterion. I have shot the query like the following to fetch the qualifications associated with the user ordering the qualifications by the start_date:

@user = User.find @current_user.id, :include => [:profile, {:qualifications=>{:order=>'start_date DESC'}}]

But when i run this in rails, it says that it can't find association named order. Any help is appreciated.

Thanks.

Upvotes: 0

Views: 86

Answers (2)

hurikhan77
hurikhan77

Reputation: 5931

If you want to order individually per find, do it this way:

@user = User.find @current_user.id, :include => [:profile, :qualifications], :order=>'qualifications.start_date DESC'

That means, move the table name into the order option.

Upvotes: 1

roman
roman

Reputation: 11278

you can do it in your model

has_many :qualifications, :order => "start_date DESC" 

Upvotes: 1

Related Questions