0

I have a user array with an invitations_attributes array inside of it. The invitations_attributes array has another array inside of it. Here is the structure:

"user"=>{"invitations_attributes"=>{"6"=>{"email"=>""}, "7"=>{"email"=>""}, "0"=>{"email"=>"[email protected]"}, "1"=>{"email"=>""}, "2"=>{"email"=>""}, "3"=>{"email"=>""}, "4"=>{"email"=>""}, "5"=>{"email"=>""}}}}

What I want to do is get a count of invitations_attributes where the email value is not blank. So in the above case, the count would be 1.

1 Answer 1

3

What you show is a nested hash, not an array. Given a user hash as you describe:

user["invitation_attributes"].count{ |key, value| value["email"].present? }
Sign up to request clarification or add additional context in comments.

3 Comments

this worked! can you explain why you put item[1] there? how come that is a variable?
I revised the answer. If you only pass in one argument to a block on a hash, it'll give you arrays, so the first one would have been ["6", {"email" => ""}], so the item at location 1 would be {"email" => ""}. However, the better way is to give two arguments, the key and value, and then you can just check the value.
hmmm...very interesting. I will have to review how this works but it worked great. thanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.