I have an active record set corresponding to some records and I want to pull particular members by index in the collection from this result (i.e. I want the 2nd, 4th and 5th record)
While this works fine:
random_members = club.users[1,3,5]
This does not:
selected_indices = [1,3,5]
random_members = club.users(selected_indices)
I need the second method since the indices I’m using are generated.
In the first case Ruby is clearly taking applying the indices to the collection but in the second it’s trying to use the array as a method argument. This is more of a Ruby question than a rails question however I have no idea what operator to use apply the array to the collection rather than calling it as a method.
NB The indices are the positions of the members in the active record set and not the ID of the actual members.
Try this:
*is called the splat operator and allows you to use the contents of an array as parameters. Think of it as the contents of the array without the[].