尝试将条目保存到我的数据库时出现以下错误:
ArgumentError: Unknown key: class
当我尝试定义PageIcon
我的Page
模型属于哪个时,就会发生这种情况。
页面迁移:
class CreatePages < ActiveRecord::Migration
def change
create_table :pages do |t|
t.integer "parent_page_id"
t.integer "page_image_id", :null => false
t.integer "page_icon_id"
t.integer "template_id"
t.string "title", :null => false
t.string "path", :default => "", :null => false
t.string "group"
t.datetime "deleted_at"
t.boolean "visible", :default => false
t.timestamps
end
add_index("pages", "parent_page_id")
add_index("pages", "page_image_id")
add_index("pages", "page_icon_id")
add_index("pages", "template_id")
add_index("pages", "title")
add_index("pages", "path")
end
end
页面模型:
class Page < ActiveRecord::Base
belongs_to :parent, :class_name => 'Page', :foreign_key => 'parent_page_id'
has_many :children, :class_name => 'Page', :foreign_key => 'parent_page_id'
belongs_to :page_image, :class_name => 'Image', :foreign_key => 'page_image_id'
has_many :sections
has_many :attributes
belongs_to :page_icon, :class_name => 'PageIcon', :foreign_key => 'page_icon_id'
validates_uniqueness_of :title, :case_sensitive => false
end
页面图标迁移
class CreatePageIcons < ActiveRecord::Migration
def change
create_table :page_icons do |t|
t.string 'title', :null => false
t.string 'description'
t.string 'url', :null => false
t.timestamps
end
end
end
页面图标模型:
class PageIcon < ActiveRecord::Base
has_many :pages, :class_name => 'Page', :foreign_key => 'page_icon_id'
end
当我在 Rails 控制台中输入以下内容时,出现错误:
p = Page.find(1)
p.page_icon = PageIcon.find(1)
p.save
错误:
ArgumentError: Unknown key: class
完整日志:
1.9.3-p392 :001 > p = Page.find(4)
Page Load (0.8ms) SELECT `pages`.* FROM `pages` WHERE `pages`.`id` = 4 LIMIT 1
=> #<Page id: 4, parent_page_id: nil, page_image_id: 1, page_icon_id: nil, template_id: nil, title: "Food", path: "", group: nil, deleted_at: nil, visible: true, created_at: "2013-03-29 12:21:31", updated_at: "2013-03-29 12:21:31">
1.9.3-p392 :002 > p.page_icon = PageIcon.find(2)
PageIcon Load (0.3ms) SELECT `page_icons`.* FROM `page_icons` WHERE `page_icons`.`id` = 2 LIMIT 1
=> #<PageIcon id: 2, title: "Food", description: nil, url: "page_icons/food.svg", created_at: "2013-03-30 01:37:13", updated_at: "2013-03-30 01:37:13">
1.9.3-p392 :003 > p.save
(0.2ms) BEGIN
Page Exists (0.5ms) SELECT 1 AS one FROM `pages` WHERE (`pages`.`title` = 'Food' AND `pages`.`id` != 4) LIMIT 1
(0.2ms) ROLLBACK
ArgumentError: Unknown key: class
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/core_ext/hash/keys.rb:51:in `block in assert_valid_keys'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/core_ext/hash/keys.rb:50:in `each_key'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/core_ext/hash/keys.rb:50:in `assert_valid_keys'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/associations/builder/association.rb:33:in `validate_options'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/associations/builder/association.rb:24:in `build'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/autosave_association.rb:139:in `build'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/associations/builder/belongs_to.rb:14:in `build'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/associations/builder/association.rb:12:in `build'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/associations.rb:1431:in `belongs_to'
from /Users/RyanKing/Sites/gastromica/app/models/attribute.rb:3:in `<class:Attribute>'
from /Users/RyanKing/Sites/gastromica/app/models/attribute.rb:1:in `<top (required)>'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:469:in `load'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:469:in `block in load_file'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:639:in `new_constants_in'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:468:in `load_file'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:353:in `require_or_load'
... 35 levels...
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/validations.rb:50:in `save'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/attribute_methods/dirty.rb:22:in `save'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/transactions.rb:259:in `block (2 levels) in save'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/transactions.rb:208:in `transaction'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/transactions.rb:259:in `block in save'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/transactions.rb:270:in `rollback_active_record_state!'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/transactions.rb:258:in `save'
from (irb):3
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
from /Users/RyanKing/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3-p392 :004 >
当我删除&模型之间的has_many
andbelongs_to
关系时,一切正常。有谁知道这里发生了什么?据我所知,语法是正确的。Page
PageIcons
更新
属性是rails中的保留字,如下所示:http ://www.ruby-forum.com/topic/2722528
So changing has_many :attributes
to has_many :page_attributes, class_name="Attribute"
in the Page
model does the trick.