I'm pulling all associated objects of a particular object, and doing an each_with_index
on it.
我将拉出一个特定对象的所有相关对象,并在其上执行each_with_index。
ie. subscription.transactions.each_with_index
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.
但是当只有一个事务(因此它不是数组)时,我得到一个未定义的方法“each”错误。
How do I get around this and basically just run the each
once?
我如何绕过这个,只运行一次?
2 个解决方案
#1
9
You can do
你可以做
Array(subscription.transactions).each_with_index
#2
4
You can also do this
你也可以这样做
[subscription.transactions].flatten.each_with_index
#1
9
You can do
你可以做
Array(subscription.transactions).each_with_index
#2
4
You can also do this
你也可以这样做
[subscription.transactions].flatten.each_with_index