Dynamoid / dynamoid

Ruby ORM for Amazon's DynamoDB.
MIT License
580 stars 195 forks source link

undefined method `symbolize_keys' when saving dynamo model #664

Closed MartyIce closed 1 year ago

MartyIce commented 1 year ago

I'm trying to migrate data from sql to dynamodb in a migration script. With the following migration:

Model Definition:

class DynamoSong

    include Dynamoid::Document

    table(
        name: "dynamo_song",
        key: :id
      )

    field :user_id, :integer
    field :title
    field :artist
    field :starred, :boolean

    global_secondary_index hash_key: :user_id
end

Migration:

require 'dynamoid'
require 'activerecord-import'

class PortSongsToDynamo < ActiveRecord::Migration[7.0]
  def up
      sql_records = ActiveRecord::Base.connection.select_all('SELECT * FROM songs')
      dynamo_records = sql_records.map do |sql_record|
        DynamoSong.new(sql_record)
      end
      DynamoSong.import dynamo_records
    end
  end

I get the following error:

undefined method `symbolize_keys' for #<DynamoSong id: "1", user_id: 1, title: "Gallows Pole", artist: nil, difficulty: "7", notes: "Good fingerpicking exercise.  Want to branch this into other versions (eg, Leadbelly's)", tuning: "1", tablature: "", capo: 3, proficiency: "0", starred: true, created_at: Sun, 21 May 2023 14:17:46 +0000, updated_at: Mon, 29 May 2023 20:29:17 +0000>
/home/martymavis/work/jamboree-hero/db/migrate/20230602191035_port_songs_to_dynamo.rb:16:in `up'

Caused by:
NoMethodError: undefined method `symbolize_keys' for #<DynamoSong id: "1", user_id: 1, title: "Gallows Pole", artist: nil, difficulty: "7", notes: "Good fingerpicking exercise.  Want to branch this into other versions (eg, Leadbelly's)", tuning: "1", tablature: "", capo: 3, proficiency: "0", starred: true, created_at: Sun, 21 May 2023 14:17:46 +0000, updated_at: Mon, 29 May 2023 20:29:17 +0000>

Rails 7.0.4.3 Ruby: ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [x86_64-linux]

andrykonchin commented 1 year ago

~Could you provide a backtrace of the exception and Dynamoid version?~

The import method accepts an array of Hashes, not instantiated models, e.g.

users = User.import([{ name: 'a' }, { name: 'b' }])
MartyIce commented 1 year ago

That ^ fixed the problem. Thank you!