What you are looking for, split :
[1] pry(main)> a = '19,20,4,1,1,4,1' => "19,20,4,1,1,4,1" [2] pry(main)> a.split(',') => ["19", "20", "4", "1", "1", "4", "1"] [3] pry(main)> a.split(',').map(&:to_i) => [19, 20, 4, 1, 1, 4, 1]
But, since you are building the "string" manually, it is better to use an array:
items = Array.new items << @invitation_user1.id << @invitation_user2.id << @user1.id items << @user2.id << @user2.id << @profile1.id << @profile2.id @activities = Version.where(item_id: items)
Anyway, the way to get the ids is weird, because ... what happens if you add more users? or profiles?
What would I do (seeing as little as possible in your example)
items = Array.new items << get_invitation_user_ids items << get_user_ids items << get_profile_ids @activities = Version.where(item_id: items)
and then define these methods, for example:
def get_invitation_user_ids InvitationUser.select(:id).map(&:id) end ...