How to sort the result that I have found according to relevance. Let''s say i have find all the users @users=User.find(:all) now in my view file I want to give a drop down box like sort ascending,sort descending, sort by date which will cause the @users sort accordingly. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
use something like @users=User.find(:all, :order => "created_at DESC") where :order is a SQL fragment, defining the sort order --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Then in your view build your select tag (drop down menu) so that every entry links to a function which calls index with a params value: def index params[:order] ||= "ccreated_at DESC" @users = User.find(:all, :order => params[:order]) end If you want to be uber picky about it you can have 2 drop down menus one of which specifies the column to order by and another (drop down or radio buttons) that say if its ASCending or DESCending and build your query in a similar type of way. def index params[:order_column] ||= "created_at" params[:direction] ||= "ASC" @users = User.find(:all, :order => "#{params[:order_column]} #{params[:direction]}) end On Jul 1, 2:07 pm, Thorsten Müller <thors...-1oxKqHKwyltBDgjK7y7TUQ@public.gmane.org> wrote:> use something like > @users=User.find(:all, :order => "created_at DESC") > where :order is a SQL fragment, defining the sort order--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you very much guys. But what I trying to do is that I don''t have to go through the database yet another time when is choose sort by date, sort asc or sort desc as the result is in @user object. Can not I just manipulate this @user object only? Any suggestion will be highly appreciated. Thanks in advance On Jul 1, 6:32 pm, "\"Wolas!\"" <jcpen...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Then in your view build your select tag (drop down menu) so that every > entry links to a function which calls index with a params value: > > def index > params[:order] ||= "ccreated_at DESC" > @users = User.find(:all, :order => params[:order]) > end > > If you want to be uber picky about it you can have 2 drop down menus > one of which specifies the column to order by and another (drop down > or radio buttons) that say if its ASCending or DESCending and build > your query in a similar type of way. > > def index > params[:order_column] ||= "created_at" > params[:direction] ||= "ASC" > @users = User.find(:all, :order => "#{params[:order_column]} > #{params[:direction]}) > end > > On Jul 1, 2:07 pm, Thorsten Müller <thors...-1oxKqHKwyltBDgjK7y7TUQ@public.gmane.org> wrote: > > > use something like > > @users=User.find(:all, :order => "created_at DESC") > > where :order is a SQL fragment, defining the sort order--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Thank you very much guys. But what I trying to do is that I don''t have > to go through the database yet another time when is choose sort by > date, sort asc or sort desc as the result is in @user object. Can not > I just manipulate this @user object only? Any suggestion will be > highly appreciated."When I choose" is the keypoint here. Say you have done your query and a @user object. Then it''s rendered and sent to the browser. The server immediately forgets about it and the @user is gone. If you select another search option in the browser and request the list, it will have to be rebuild anyway. Even if this would not be the case: Sorting with Ruby code is in nearly any case slower than a db request. Avoid it if you can. Even if you would display two lists of the same object in one page, it would be most likely much faster to do two db requests. If it''s in the db, let the db handle the heavy work. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Thorsten Muller. I highly appreciate your suggestion and logics you have pointed. Thank you On Jul 2, 1:38 pm, Thorsten Müller <thors...-1oxKqHKwyltBDgjK7y7TUQ@public.gmane.org> wrote:> > Thank you very much guys. But what I trying to do is that I don''t have > > to go through the database yet another time when is choose sort by > > date, sort asc or sort desc as the result is in @user object. Can not > > I just manipulate this @user object only? Any suggestion will be > > highly appreciated. > > "When I choose" is the keypoint here. > Say you have done your query and a @user object. > Then it''s rendered and sent to the browser. The server immediately > forgets about it and the @user is gone. > If you select another search option in the browser and request the > list, > it will have to be rebuild anyway. > Even if this would not be the case: Sorting with Ruby code is > in nearly any case slower than a db request. Avoid it if you can. > > Even if you would display two lists of the same object in one page, > it would be most likely much faster to do two db requests. > > If it''s in the db, let the db handle the heavy work.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---