Reputation: 1783
I have an array, and I want to filter for example by :city. Using ActiveRecord query it would be something like this:
MyBase.where(city: "NY")
How can I filter an array without ActiveRecord method, using only pure Ruby?
Upvotes: 0
Views: 49
Reputation: 118261
You should use Array#select method.
array_of_objects.select { |o| o.city == "NY" }
Example :
Person = Struct.new(:name, :city)
array_of_persons = [ Person.new('A', 'foo'), Person.new('B', 'boo') ]
array_of_persons.select { |person| person.city == 'foo' }
# => [#<struct Person name="A", city="foo">]
Upvotes: 1