How to convert array output to regular string in ruby ​​on rails application

I implement a tag function, and an article can contain from one to many tags. I can get tag values ​​from db in this format

["social network", "professional"] 

I need output in this format

 "social network professional" 

I want to convert an array to a string without,. Below is a code snippet that extracts values ​​from db as an array.

 <%= article.tags.collect(&:name) %> 

How can I convert this output to a string value without any comma?

+4
source share
2 answers

Have you looked pluck ? This is very useful if you only need one entry from db (in your case "name"). You can use this for this:

 a = article.tags.pluck(:name) 

To then display the names of your article, separated by spaces, follow these steps:

 a.join(" ") 

For completeness, you can link these methods (as you said in a comment below) as follows:

 article.tags.pluck(:name).join(" ") 
+11
source

I have two solutions that are below:

 <%= article.tags.collect(&:name).join(" ")%> <%= article.tags.pluck(:name).join(" ") %> - by yossarian. 
0
source

All Articles