Can't store array in json field in postgresql (rails) can't cast Array to json

Posted by Drew H on Stack Overflow See other posts from Stack Overflow or by Drew H
Published on 2013-06-29T21:50:27Z Indexed on 2013/07/01 23:05 UTC
Read the original article Hit count: 516

This is the error I'm getting when I run db:migrate

rake aborted!
can't cast Array to json

This is my table

  class CreateTrips < ActiveRecord::Migration

          def change
            create_table :trips do |t|

              t.json :flights
              t.timestamps
            end
          end 
        end

This is in my seeds.rb file

flights = [{
    depart_time_hour: 600,
    arrive_time_hour: 700,

    passengers: [
        {
            user_id: 1,
            request: true    
        }
    ]
}]

trip = Trip.create(
  {
    name: 'Flight',

    flights: flights.to_json 
  }
)

For some reason I can't do this. If I do this.

trip = Trip.create(
      {
        name: 'Flight',
        flights: { flights: flights.to_json }
      }
    )

It works. I don't want this though because now I have to access the json array with trip.flights.flights. Not the behavior I'm wanting.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby