/ Published in: Ruby
Useful for when you need to do a find and map specific results to an array for use later
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#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' }
URL: http://errtheblog.com/post/608