Database value is
["", "5", "1", "2", "8", "6", "9"]
But in Rails console it showing as:
"[\"\", \"5\", \"1\", \"2\", \"8\", \"6\", \"9\"]"
Any solution to show it as same as the array?
Looks like you column is saved as json
[15] pry(main)> JSON.parse("[\"\", \"5\", \"1\", \"2\", \"8\", \"6\", \"9\"]")
=> ["", "5", "1", "2", "8", "6", "9"]
you can use serialize :name_of_that_column, Array
so it get parsed when called from db
you can check http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html f
If what you want do is getting array of integers from string, use scan
and regular expression.
For example,
pry(main)> str = "[\"\", \"5\", \"1\", \"2\", \"8\", \"6\", \"9\"]"
=> "[\"\", \"5\", \"1\", \"2\", \"8\", \"6\", \"9\"]"
pry(main)> str.scan(/\d+/)
=> ["5", "1", "2", "8", "6", "9"]
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.