Reputation: 2917
I get this two array of hashes after performing join
array 1
[#<State id: 1, name: "Alabama">, #<State id: 1, name: "Alabama">, #<State id: 1, name: "Alabama">, #<State id: 1, name: "Alabama">, #<State id: 2, name: "Alaska">, #<State id: 2, name: "Alaska">, #<State id: 4, name: "Arkansas">, #<State id: 4, name: "Arkansas">, #<State id: 4, name: "Arkansas">, #<State id: 6, name: "Colorado">, #<State id: 6, name: "Colorado">, #<State id: 6, name: "Colorado">, #<State id: 11, name: "Georgia">, #<State id: 14, name: "Illinois">, #<State id: 18, name: "Kentucky">, #<State id: 18, name: "Kentucky">, #<State id: 22, name: "Massachusetts">, #<State id: 48, name: "Washington">]
array 2
[#<City id: 1, name: "Abbeville", state_id: 1>, #<City id: 1, name: "Abbeville", state_id: 1>, #<City id: 1, name: "Abbeville", state_id: 1>, #<City id: 4543, name: "Abingdon", state_id: 14>, #<City id: 8282, name: "Accord", state_id: 22>, #<City id: 3808, name: "Acworth", state_id: 11>, #<City id: 6855, name: "Adairville", state_id: 18>, #<City id: 6855, name: "Adairville", state_id: 18>, #<City id: 18895, name: "Adams County", state_id: 6>, #<City id: 4, name: "Addison", state_id: 1>, #<City id: 4, name: "Addison", state_id: 1>, #<City id: 17510, name: "Addy", state_id: 48>, #<City id: 1054, name: "Adona", state_id: 4>, #<City id: 1054, name: "Adona", state_id: 4>, #<City id: 577, name: "Akiachak", state_id: 2>, #<City id: 1056, name: "Alicia", state_id: 4>, #<City id: 583, name: "Ambler", state_id: 2>, #<City id: 2783, name: "Aspen", state_id: 6>]
I want to make a third array from the above two based on the value of state_id in each array
in this case for example [#, .... and so on
for your help the first two hashes array i got using join query
@states = State.joins("INNER JOIN property_of_interests ON property_of_interests.state_id = states.id").where(:property_of_interests => {:user_id => current_user.id})
@cities = City.joins("INNER JOIN property_of_interests ON property_of_interests.city_id = cities.id").where(:property_of_interests => {:user_id => current_user.id})
can I work on the query itself to get the desired output ?.
I tried something like
`@states.select("@states.name,@cities.name").joins("INNER JOIN @cities ON @cities.state_id = @states.id")`
but it doesnt work.
More Information
states id, name
cities id, name, state_id
property_of_interests id, user_id, state_id, state_name
Desired output like
State Name City Name
Alabama Abbeville
Alabama Abbeville
Alabama Abbeville
....
Upvotes: 0
Views: 83
Reputation: 4478
You would do something like:
City.all.each do |city|
puts "#{city.state.name} #{city.name}"
end
Alternately, as an array:
arr = City.all.map { |c| [c.state.name, c.name] }
Or as an array of hashes:
arr = City.all.map { |c| {state: c.state.name, city: c.name} }
Or to actually answer the question, since you want to start with the properties_of_interest table:
PropertyOfInterest.all.each do |prop|
prop.state.cities.each do |city|
puts prop.state.name, city.name
end
end
Upvotes: 1