I have a table in database, name of table is tags
and This table has 2 columns: 1.name, 2.count. I want read first column and add to an array and seprate names with ","
. I use below code:
@list = Array.new
temp = Tag.all
@list || temp.map(&:name).join(",")
I want create list like below sample:
"name1","name2",..,"namen"
I want pass this array to a javascript. I asked a question
; I want create array type like this: ["name1","name2"]. How can I convert @list to this type?
Tag.pluck(:name)
will create an array of names from the tags
database
To add to an array , you'll want to do this:
@list ||= Tag.all.map(&:name)
@list << Tag.find 15 # -> if you wanted to add to the array
Arrays are already joined with a ,
- you don't need to do that. If you wanted a string of names which is joined by ,
, you'll want to convert the array to a string first
if you have rails 3.2~> then use
Tag.pluck(:name)
and if your rails old then use
@list = Tag.all.map(&:name).join(",")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.