我是Rails的初学者,我在使用Rails的迁移将行插入数据库时遇到了问题。
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运行此代码将导致:
== 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}操作模型:
class Actions < ActiveRecord::Base
belongs_to :channels
attr_accessible :name, :description, :weight, :channel_id
end我不知道花括号是从哪里来的,也不知道它们为什么会导致异常。谁能帮我解决这个问题?
发布于 2012-07-11 04:27:21
哦哦,您的迁移类名似乎与您试图访问的模型(Actions)的名称相同。因此,将在迁移类上调用create方法,而不是模型类,它可能会尝试使用您的散列或其他方法创建表。这就是您收到该错误消息的原因。
重命名您的迁移类(以及它的文件,以保持一致性),它应该可以正常运行:
class CreateActions < ActiveRecord::Migrationhttps://stackoverflow.com/questions/11421295
复制相似问题