I have a test spec where I use the following line of code to assign 3 variables to session tokens within my table:
@auth_token, @auth2_token, @auth3_token = Session.limit(3).map(&:token)
I now wish to assign 3 variables as a role classes from my Roles table which isn’t restricted to one attribute only but the whole class. I have tried the following but it doesnt seem to be working:
@role1, @role2, @role3 = Role.limit(3).map
Can this be achieved? Any pointers would be greatly appreciated !!
It works for the auth tokens because map converts the relation object into an array which then gets assigned to the variables. For the roles just calling map returns an enumerable and not an array.
You can just call to_a directly on the relation object returned by the limit call in order to convert it to an array.
@role1, @role2, @role3 = Role.limit(3).to_a