Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Argument Error on .save - Something to do with has_many belongs_to in Model
    primarykey
    data
    text
    <p>I get the following error when trying to save an entry into my database:</p> <p><code>ArgumentError: Unknown key: class</code></p> <p>This happens when I try to define which <code>PageIcon</code> my <code>Page</code> model belongs to.</p> <hr> <p>Page Migration:</p> <pre><code>class CreatePages &lt; ActiveRecord::Migration def change create_table :pages do |t| t.integer "parent_page_id" t.integer "page_image_id", :null =&gt; false t.integer "page_icon_id" t.integer "template_id" t.string "title", :null =&gt; false t.string "path", :default =&gt; "", :null =&gt; false t.string "group" t.datetime "deleted_at" t.boolean "visible", :default =&gt; 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 </code></pre> <p>Page Model:</p> <pre><code>class Page &lt; ActiveRecord::Base belongs_to :parent, :class_name =&gt; 'Page', :foreign_key =&gt; 'parent_page_id' has_many :children, :class_name =&gt; 'Page', :foreign_key =&gt; 'parent_page_id' belongs_to :page_image, :class_name =&gt; 'Image', :foreign_key =&gt; 'page_image_id' has_many :sections has_many :attributes belongs_to :page_icon, :class_name =&gt; 'PageIcon', :foreign_key =&gt; 'page_icon_id' validates_uniqueness_of :title, :case_sensitive =&gt; false end </code></pre> <hr> <p>PageIcon Migration</p> <pre><code>class CreatePageIcons &lt; ActiveRecord::Migration def change create_table :page_icons do |t| t.string 'title', :null =&gt; false t.string 'description' t.string 'url', :null =&gt; false t.timestamps end end end </code></pre> <p>PageIcon Model:</p> <pre><code>class PageIcon &lt; ActiveRecord::Base has_many :pages, :class_name =&gt; 'Page', :foreign_key =&gt; 'page_icon_id' end </code></pre> <hr> <p>When I enter the following into rails console I get the error:</p> <pre><code>p = Page.find(1) p.page_icon = PageIcon.find(1) p.save </code></pre> <p>Error: <code>ArgumentError: Unknown key: class</code></p> <hr> <p>Full log:</p> <pre><code>1.9.3-p392 :001 &gt; p = Page.find(4) Page Load (0.8ms) SELECT `pages`.* FROM `pages` WHERE `pages`.`id` = 4 LIMIT 1 =&gt; #&lt;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"&gt; 1.9.3-p392 :002 &gt; p.page_icon = PageIcon.find(2) PageIcon Load (0.3ms) SELECT `page_icons`.* FROM `page_icons` WHERE `page_icons`.`id` = 2 LIMIT 1 =&gt; #&lt;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"&gt; 1.9.3-p392 :003 &gt; 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 `&lt;class:Attribute&gt;' from /Users/RyanKing/Sites/gastromica/app/models/attribute.rb:1:in `&lt;top (required)&gt;' 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 `&lt;top (required)&gt;' from script/rails:6:in `require' from script/rails:6:in `&lt;main&gt;'1.9.3-p392 :004 &gt; </code></pre> <hr> <p>When I remove the <code>has_many</code> and <code>belongs_to</code> relationships between the <code>Page</code> &amp; <code>PageIcons</code> models everything works fine again. Does anyone know what going on here? As far as I can tell the syntax is correct.</p> <hr> <p>UPDATE</p> <p>Attribute is reserved word in rails as pointed out here: <a href="http://www.ruby-forum.com/topic/2722528" rel="nofollow">http://www.ruby-forum.com/topic/2722528</a></p> <p>So changing <code>has_many :attributes</code> to <code>has_many :page_attributes, class_name="Attribute"</code> in the <code>Page</code> model does the trick.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload