So, I have a User has_many :profiles. The most recently dated profile is considered the current one. I need to sort these users by name, but the name attribute is a member of the associated table. Is there any easy way to do this other than with some ugly find_by_sql? Maybe a more general question - is there a common solution to sorting in Ruby? -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2006-Jul-01 14:37 UTC
[Rails] Sorting by an attribute on a associated table
What about using acts_as_list, :order => :date_added (or whatever you call that column)? Then you could say def profile profilies.first end .... I think :) On Jul 1, 2006, at 9:17 AM, Bryan Duxbury wrote:> So, I have a User has_many :profiles. The most recently dated > profile is > considered the current one. I need to sort these users by name, but > the > name attribute is a member of the associated table. > > Is there any easy way to do this other than with some ugly > find_by_sql? > > Maybe a more general question - is there a common solution to > sorting in > Ruby? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Chris Carter
2006-Jul-01 14:38 UTC
[Rails] Re: Sorting by an attribute on a associated table
Something like @user.profiles.find(:all, :order => "name DESC") Bryan Duxbury wrote:> So, I have a User has_many :profiles. The most recently dated profile is > considered the current one. I need to sort these users by name, but the > name attribute is a member of the associated table. > > Is there any easy way to do this other than with some ugly find_by_sql? > > Maybe a more general question - is there a common solution to sorting in > Ruby?-- Posted via http://www.ruby-forum.com/.
Bryan Duxbury
2006-Jul-01 14:45 UTC
[Rails] Re: Sorting by an attribute on a associated table
Perhaps I was unclear. I have no trouble sorting the profiles themselves. What I want to do is sort the USERS by an attribute on their most recent profile. Something along the lines of: @users = User.find(:all, :order => |user| {user.profile.firstname}) Of course I totally made that syntax up. -- Posted via http://www.ruby-forum.com/.
Matthew Margolis
2006-Jul-01 14:51 UTC
[Rails] Re: Sorting by an attribute on a associated table
Bryan Duxbury wrote:> Perhaps I was unclear. I have no trouble sorting the profiles > themselves. > > What I want to do is sort the USERS by an attribute on their most recent > profile. Something along the lines of: > > @users = User.find(:all, :order => |user| {user.profile.firstname}) > > Of course I totally made that syntax up. > >I don''t know how to do this in SQL but I can give you some ruby that will get you going. @users = User.find(:all) @users.sort_by {|user| user.profile.firstname} This will be really slow compared to a pure SQL sort so I would still look for a way to do that. Matthew Margolis blog.mattmargolis.net
Bryan Duxbury
2006-Jul-01 15:16 UTC
[Rails] Re: Re: Sorting by an attribute on a associated table
Matthew Margolis wrote:> Bryan Duxbury wrote: >> > I don''t know how to do this in SQL but I can give you some ruby that > will get you going. > > @users = User.find(:all) > @users.sort_by {|user| user.profile.firstname} > > This will be really slow compared to a pure SQL sort so I would still > look for a way to do that. > > Matthew Margolis > blog.mattmargolis.netAwesome, that''s exactly what I was looking for. Thanks! -- Posted via http://www.ruby-forum.com/.
Richard Livsey
2006-Jul-02 17:51 UTC
[Rails] Re: Sorting by an attribute on a associated table
Matthew Margolis wrote:> Bryan Duxbury wrote: >> Perhaps I was unclear. I have no trouble sorting the profiles themselves. >> >> What I want to do is sort the USERS by an attribute on their most >> recent profile. Something along the lines of: >> >> @users = User.find(:all, :order => |user| {user.profile.firstname}) >> >> Of course I totally made that syntax up. >> > I don''t know how to do this in SQL but I can give you some ruby that > will get you going. > > @users = User.find(:all) > @users.sort_by {|user| user.profile.firstname} > > This will be really slow compared to a pure SQL sort so I would still > look for a way to do that. >Something like this should do the trick in pure SQL. @users = User.find(:all, :include => :profile, :order => "profiles.firstname ASC") Or just do a join if you don''t want to instantiate all the profiles. hth -- R.Livsey http://livsey.org