Robin
Robin

Reputation: 313

Rails Model.create SQLite3::SQLException

I'm a beginner in Rails, and I'm having trouble inserting rows into the database using Rails's migration.

class Actions < ActiveRecord::Migration
  def up
    create_table :actions do |t|
      t.integer :channel_id
      t.string :name
      t.text :description
      t.integer :weight

      t.timestamps
    end

    add_index :actions, :channel_id

    Actions.create :name => 'name', :description => '', :weight => 1, :channel_id => 1
  end

Running this code results in:

==  Actions: migrating ========================================================
-- create_table(:actions)
   -> 0.0076s
-- add_index(:actions, :channel_id)
   -> 0.0036s
-- create({:name=>"name", :description=>"", :weight=>1, :channel_id=>1})
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: unrecognized token: "{": {:name=>"name", :description=>"", :weight=>1, :channel_id=>1}

The Action model:

class Actions < ActiveRecord::Base
  belongs_to :channels
  attr_accessible :name, :description, :weight, :channel_id
end

I don't know where the curly brackets come from and why they cause an exception. Who can help me solving this problem?

Upvotes: 2

Views: 568

Answers (1)

Mate Solymosi
Mate Solymosi

Reputation: 5967

Uh oh, it seems that your migration class name is the same as the name of the model you're trying to access (Actions). Because of this, instead of the model class, the create method will be called on the migration class, which probably tries to create a table using your hash, or something. That's why you're getting that error message.

Rename your migration class (and also its file for the sake of consistency) and it should run fine:

class CreateActions < ActiveRecord::Migration

Upvotes: 3

Related Questions