Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Unknown key: polymorphic" while running rake

I want to implement a simple polymorphy. My goal is that user, group, and maybe other models can share a single pool of IDs, so that everything represents a GlobalObject. I built something like this:

  • global_object.rb generated by $ rails generate scaffold GlobalObject mode:integer:

    class GlobalObject < ActiveRecord::Base
      attr_accessible :mode
      has_many :identifyable, :polymorphic => true, :dependent => :destroy
    end
    
  • user.rb generated by $ rails generate scaffold User login password:

    class User < ActiveRecord::Base
      attr_accessible :login, :password, :salt
      belongs_to :global_object, :as => :permittable
    end
    

And a group model would follow with a very similar structure. I didn't modify the migrate files from the db folder, but just executed $ rake db:migrate.

  • 20131102224115_create_global_objects.rb:

    class CreateGlobalObjects < ActiveRecord::Migration
      def change
        create_table :global_objects do |t|
          t.integer :mode
          t.timestamps
        end
      end
    end
    
  • 20131102224332_create_users.rb:

    class CreateUsers < ActiveRecord::Migration
      def change
        create_table :users do |t|
          t.string :login
          t.string :password
          t.timestamps
        end
      end
    end
    

When I execute $ rake, it shows two impressive traces with these beginnings:

(...)hash/keys.rb:51:in `block in assert_valid_keys': Unknown key: polymorphic (ArgumentError)
    from /Users(...)
    ...

(...)hash/keys.rb:51:in `block in assert_valid_keys': Unknown key: polymorphic (ArgumentError)
    from /Users(...)
    ...

When I remove the :polymorphic => true in global_object.rb, the same errors occur, but instead of Unknown key: polymorphic, it says Unknown key: as. What is wrong?


1 Answers

There are several problems here:

  • You are missing the polymorphic relationship columns in you migration that should refer to identifyable_id and identifyable_type.
  • You are referring to the polymorphic association by different names - identifyable and permittable.
  • Polymorphic relationships are meant to be set on the child elements (belongs_to) and referred to by multiple parent classes (has_many). You have it the other way around which is not supported (and wouldn't really make sense from a modeling perspective).

It would be helpful to understand what a GlobalObject is meant to accomplish to help determine if polymorphic associations are the correct approach here. But if you wanted to use them, you'd either need to make a GlobalObject belong_to identifyable (and add the necessary columns there), or you'd need to move it to the User and add the columns there.

like image 54
PinnyM Avatar answered Jun 11 '26 16:06

PinnyM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!