Sort array of ActiveRecord object without querying to database.

Sometimes it may happen you want to sort the array of objects, fetched from ActiveRecord.

e.g. I have a users table having name column after I done any manipulation on the fetched object.

You can just call the following code for doing the above stuff.

@users = User.find :all
@users = @users – xyz_obj.users #remove existing users.
@users = @users.sort_by { |user| user[:name] }

May this code helps anyone….

Thanks ;-)

You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.