Reputation: 1621
I have a method that is suppose to iterate over an array and match any items to a string I have in my model. My method looks like this
@new_array = @old_array.find_all { |t| t.fetch('name') == "self.object_name" }
This method should look thru the array of hashes I have and match any items that have the same name as object_name. When I test the name comparison to the object_name in console it shows true but when I run the full method described above it shows no objects found, however I know the array contains multiple objects with the EXACT same name. Any idea of whats wrong?
The array looks like so...
old_array = {"id"=>"123", "account"=>"456", "name"=>"CITY"},
{"id"=>"456", "account"=>"567", "name"=>"CITY DIR DEP"},
{"id"=>"456", "account"=>"567", "name"=>"BUCK"},
{"id"=>"456", "account"=>"567", "name"=>"CITY DIR DEP"},
{"id"=>"456", "account"=>"567", "name"=>"HAPPY"},
{"id"=>"456", "account"=>"567", "name"=>"CIRCLE"}
and the object prints out in console as
self.object_name => "CITY DIR DEP"
Upvotes: 0
Views: 135
Reputation: 44581
You don't need quotes "
at all (you literally trying to compare retrieved name
with string "self.object_name"
instead of the value of self.object_name
):
@new_array = @old_array.find_all { |t| t.fetch('name') == self.object_name }
If you are a big fan, you can interpolate with "#{}"
:
@new_array = @old_array.find_all { |t| t.fetch('name') == "#{self.object_name}" }
Upvotes: 2
Reputation: 389
Try this:
@new_array = @old_array.find_all { |t| t['name'] == self.object_name }
Upvotes: 1