2

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?

4
  • 2
    You should be naming your model classes singular, i.e. class Action < ActiveRecord::Base. Commented Jul 10, 2012 at 20:24
  • You're welcome. Remember: IN GENERAL, models are singular and controllers are plural. Commented Jul 10, 2012 at 20:40
  • Could you elaborate on the 'in general' comment? Are there cases in which you would do this differently? Commented Jul 10, 2012 at 20:43
  • I think Chris meant that singular model names are the Rails convention, which you should but don't have to follow every time. For example, if you have a table called user_details, it would be logical to call its model UserDetails instead of just UserDetail. Commented Jul 10, 2012 at 20:55

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

... and take Chris' advice in the comments :)
Thank you, trying this now. I wonder why the migration class creates a hash of this specific piece of code. Any ideas?
It does not create a hash, you provide it in this line: Actions.create :name => 'name', :description => '', :weight => 1, :channel_id => 1 <= the parameters here are converted to a hash by the Ruby interpreter and that gets passed to the method. But this is most likely unrelated to your problem (colliding class names, and therefore calling the wrong create method).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.