Reputation: 4526
Is it possible to save the following array without outer quotes? Or is it possible to access the array without the outer quotes?
e.hours = "5:30AM", "6:00AM", "6:30AM"
# => ["5:30AM", "6:00AM", "6:30AM"]
e.save
(0.2ms) begin transaction
SQL (1.6ms) UPDATE "brands" SET "hours" = ?, "updated_at" = ? WHERE "brands"."id" = ? [["hours", "[\"5:30AM\", \"6:00AM\", \"6:30AM\"]"], ["updated_at", "2015-09-30 00:35:25.117927"], ["id", 1]]
(6.8ms) commit transaction
# => true
e
# => #<Brand id: 1, name: "Starbucks", created_at: "2015-09-23 22:59:08", updated_at: "2015-09-30 00:35:25", hours: "[\"5:30AM\", \"6:00AM\", \"6:30AM\">
This is what the migration looks like:
add_column :brands, :hours, :string, array: true, default: []
When I try to access the array in the controller, it returns the array with outer quotes:
[1] pry(#<OrdersController>)> @delivery_hours
# => "[\"5:30AM\", \"6:00AM\", \"6:30AM\"]"
Upvotes: 0
Views: 90
Reputation: 8831
Rails stores array records in JSON format, so you just need to change JSON to array.
JSON.parse("[\"5:30AM\", \"6:00AM\", \"6:30AM\"]")
# => ["5:30AM", "6:00AM", "6:30AM"]
Upvotes: 2