ruby on rails - Interpreting output from query -
i have been stuck few hours on little problem have , thankful if help.
my ultimate goal here unique states , link each of states it's page show communities state belongs_to.
i have tried fiddling around 2 alternatives, alt.1 gives me array makes easy fit .each loop no id find communities with. alt.2 gives me id & state output in format don't recognize.
# model class community... has_one :location class location... belongs_to :community # view # alt.1 location.pluck("distinct state") # alt.2 location.select('distinct on(state) id, state') => [#<location id: 2, state: "oakland">, #<location id:4, state: "south carolina">]
solution both or alternative alternatives appreciated.
a couple of things:
1) athar said, second correct, , activerecord objects. more activerecord way location.select('state').uniq.map(&:state)
, return array of states.
2) read http://guides.rubyonrails.org/active_record_querying.html - don't think select
think does.
3) may useful in future have states table, not need populate initial values migration, instead of having string column on location. enforce location references existing state.
Comments
Post a Comment