Published in: Ruby
URL: http://errtheblog.com/post/608
Useful for when you need to do a find and map specific results to an array for use later
#Instead of using: books = Book.find(:all, :order => 'id desc', :limit => 20) comics = [] books.each do |book| comics << book if book.item_type == 'Comic' end #OR comics = Book.find(:all, :order => 'id desc', :limit => 20).map {|book| [book] unless book.item_type == 'Comic'}.flatten! #Use this: books = Book.find(:all, :order => 'id desc', :limit => 20) comics = books.select { |i| i.item_type == 'Comic' }
You need to login to post a comment.
