Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails Active Record Query (.each, .collect, .map ...?)

This is one example of one entry in my database:

Market id: 1, name: "Independence Park (Independently Run Farmers Market...", address: "3945 N. Springfield Ave., Chicago, IL", zipcode: "60618", created_at: "2013-01-01 21:22:24", updated_at: "2013-01-01 21:22:24"

All I want to do is list the 43 zipcodes from all the entries in my database. Why don't these queries work?

  1. Market.all.each { |m| m.zipcode }
  2. Market.all.zipcode
    • m = Market.all
    • m.each{ |m| m.zipcode }

Thanks!

like image 871
user1946151 Avatar asked Jan 03 '13 16:01

user1946151


1 Answers

If all you want is an array of zip codes, I would suggest to try this:

Market.pluck(:zipcode)
like image 107
Eugene Rourke Avatar answered Oct 06 '22 04:10

Eugene Rourke