I'm aware of how include?
works, however not really clear using it with multidimensional array, or a hash (is this possible with a hash?)
For example, given I have a hash that looks like:
@user.badges => [{:id => 1, :name => 'blahh', :description => 'blah blah blah'}, {:id => 2, :name => 'blahh', :description => 'blah blah blah'}]
Can I see if it has an object with the id of 1 in it like this?
if @user.badges.include?(:id => 1)
# do something
end
It doesn’t seem to work, how I can I write this method properly?
That's not valid Ruby syntax. You want:
@user.badges.any? { |b| b[:id] == 1 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With