2

I'm pulling all associated objects of a particular object, and doing an each_with_index on it.

ie. subscription.transactions.each_with_index

But when there is only one transaction (and thus it's not an array), I get an undefined method 'each' error.

How do I get around this and basically just run the each once?

2 Answers 2

9

You can do

Array(subscription.transactions).each_with_index
4

You can also do this

[subscription.transactions].flatten.each_with_index

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.