I feel like this is such an easy question
<% @state.cities.each do |city| %>
<%= city.id %>
<% end %>
puts the ids as follows:
1
2
3 etc...
How do I turn the iteration into an array?
so it outputs as follows:
[1,2,3,4,etc...]
I feel like this is such an easy question
<% @state.cities.each do |city| %>
<%= city.id %>
<% end %>
puts the ids as follows:
1
2
3 etc...
How do I turn the iteration into an array?
so it outputs as follows:
[1,2,3,4,etc...]
There is a method that does just that!
What you are looking for is the map method.
Creates a new array containing the values returned by the block.
http://apidock.com/ruby/Array/map
The documentation states, creates an array containing the values returned by a block.
@state.map do |state|
state.id
end
=> [1,2,3,...]
Which is the same as:
@state.map(&:id)
=> [1,2,3,...]
But uses the Ruby Enumerable shorthand.