0

I have this array that looks like this.

@a = ["foo","bar"].join(",")

But i would like to retrieve the "foo" and "bar" through a loop from my database instead of creating them manually and insert them into the array. How would i do that? For instance i have data, in which i want all the usernames to be put in an array and be seperated by the ",". How can i put all usernames into the array?

@data = Data.all

@data.each do |d|
   d.username
end

1 Answer 1

1
usernames = Data.all.map(&:username)
joined = usernames.join(',')
Sign up to request clarification or add additional context in comments.

1 Comment

Your first answer worked, thanks. Data.all.map(&:username).join(',')

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.