[Rails] Re: Order for a each loop

2009-06-30 Thread Colin Law

2009/6/29 Rob Biedenharn r...@agileconsultingllc.com:

 On Jun 29, 2009, at 5:31 PM, Shandy Nantz wrote:
 I have a model for users and another model for linenumbers and I have
 some code that looks like:

 @user.linenumbers.each do |line|

 end

 and it loops through a user and their line numbers. What if I want to
 order this list, is there a way to do that?

 I know I could just put these linenumbers into an a find statement
 in my
 controller and order it that way, but I would rather not do that way.
 Thanks,

 -S
 --

 Well, after first thinking Why not?  I'll offer this (it's only Ruby):

 @user.linenumbers.sort_by{|line| line.number}.each do |line|
   #...
 end

 But I'll still suggest:

 class User
   has_many :linenumbers, :order = 'number'
 end

 Or perhaps a named scope is OK,

 class Linenumber
   named_scope :ordered, :order = 'number'
 end

Or
default_scope :order = 'number'
if linenumbers are always to be sorted.

Coli9n

--~--~-~--~~~---~--~~
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Order for a each loop

2009-06-30 Thread Shandy Nantz

Perfect, exactly what I wanted, thank you both.

-S
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Order for a each loop

2009-06-29 Thread Frederick Cheung



On Jun 29, 5:31 pm, Shandy Nantz rails-mailing-l...@andreas-s.net
wrote:
 I have a model for users and another model for linenumbers and I have
 some code that looks like:

 @user.linenumbers.each do |line|

 end

 and it loops through a user and their line numbers. What if I want to
 order this list, is there a way to do that?

Well you can give an :order option to the association yourself.
Alternatively you could call sort or sort_by on your array

Fred

 I know I could just put these linenumbers into an a find statement in my
 controller and order it that way, but I would rather not do that way.
 Thanks,

 -S
 --
 Posted viahttp://www.ruby-forum.com/.
--~--~-~--~~~---~--~~
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Order for a each loop

2009-06-29 Thread Rob Biedenharn

On Jun 29, 2009, at 5:31 PM, Shandy Nantz wrote:
 I have a model for users and another model for linenumbers and I have
 some code that looks like:

 @user.linenumbers.each do |line|

 end

 and it loops through a user and their line numbers. What if I want to
 order this list, is there a way to do that?

 I know I could just put these linenumbers into an a find statement  
 in my
 controller and order it that way, but I would rather not do that way.
 Thanks,

 -S
 -- 

Well, after first thinking Why not?  I'll offer this (it's only Ruby):

@user.linenumbers.sort_by{|line| line.number}.each do |line|
   #...
end

But I'll still suggest:

class User
   has_many :linenumbers, :order = 'number'
end

Or perhaps a named scope is OK,

class Linenumber
   named_scope :ordered, :order = 'number'
end

@user.linenumbers.ordered.each do |line|
   #...
end

-Rob

Rob Biedenharn  http://agileconsultingllc.com
r...@agileconsultingllc.com



--~--~-~--~~~---~--~~
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---