Search code examples
ruby-on-railsrubysqliteactiverecordactive-record-query

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!


Solution

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

    Market.pluck(:zipcode)