Hi, I want sort list of object by field "num". I try @blocks = Block.find_all @blocks.collect{|block| block.attributes.sort_by{|item| item["num"]}} but I get TypeError: can''t convert String into Integer from (irb):37:in `[]'' from (irb):37 from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/base.rb:2068:in `sort_by'' from (irb):37:in `each'' from (irb):37:in `sort_by'' from (irb):37 from (irb):37:in `collect'' from (irb):37 How to do it correctly? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You could do: @blocks = Block.find(:all) @blocks.sort! {|a,b| a.num<=> b.num} On Dec 11, 7:53 pm, "Угодай n/a" <ugo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, I want sort list of object by field "num". > I try > @blocks = Block.find_all > @blocks.collect{|block| block.attributes.sort_by{|item| item["num"]}} > but I get > TypeError: can''t convert String into Integer > from (irb):37:in `[]'' > from (irb):37 > from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/base.rb:2068:in > `sort_by'' > from (irb):37:in `each'' > from (irb):37:in `sort_by'' > from (irb):37 > from (irb):37:in `collect'' > from (irb):37 > > How to do it correctly?--~--~---------~--~----~------------~-------~--~----~ 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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Dec-11 13:37 UTC
Re: How to sort list of object?
Hi -- On Mon, 11 Dec 2006, n/a wrote:> > Hi, I want sort list of object by field "num". > I try > @blocks = Block.find_allfind_all is deprecated in favor of find(:all). (find_all is still an OK method for enumerable classes in Ruby, like Array and Hash, but the ActiveRecord version is deprecated.)> @blocks.collect{|block| block.attributes.sort_by{|item| item["num"]}} > but I get > TypeError: can''t convert String into IntegerA hybrid AR/Ruby version would look like this: @blocks = Block.find(:all).sort_by {|b| b.num } but you can also ask the database to do the sorting work: @blocks = Block.find(:all, :order => "num") David -- Q. What''s a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.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-/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 -~----------~----~----~----~------~----~------~--~---