1

我有三个型号,正在浏览的页面,管理用户和组:在控制台上卡住(对象不支持#inspect)

class Page < ActiveRecord::Base 
    attr_accessible :name, :position, :permalink 
    belongs_to :subject 
    has_many :sections 
    has_and_belongs_to_many :editors, :class_name => "AdminUser" 
end 

class AdminUser < ActiveRecord::Base 
    attr_accessible :first_name, :last_name, :username 
    has_and_belongs_to_many :pages 
    scope :named, lamda {|first,last| where(:first_name => first, :last_name => last)} 
end 

class Section < ActiveRecord::Base 
    belongs_to :page 
end 

与这些迁移的页面,用户和节:

class CreatePages < ActiveRecord::Migration 
    def change 
    create_table :pages do |t| 
     t.references :subject 
     t.string "name" 
     t.string "permalink" 
     t.integer "position" 
     t.boolean "visible", :default => false 
     t.timestamps 

    end 

      puts "*** About to add an index ***" 
     add_index("pages", "subject_id") 
     add_index("pages", "permalink") 
    end 
end 

class CreateUsers < ActiveRecord::Migration 
    def change 
    create_table :users do |t| 
     t.string "first_name", :limit => 25 
     t.string "last_name", :limit => 50 
     t.string "email", :default => "" , :null => false 
     t.string "password", :limit => 40 
     t.timestamps 
    end 
    end 
end 


class CreateSections < ActiveRecord::Migration 
    def change 
    create_table :sections do |t| 
     t.string "name" 
     t.integer "position" 
     t.boolean "visible", :default => false 
     t.string "content_type" 
     t.text "content" 
     t.timestamps 

    end 
    puts "*** About to add an index ***" 
     add_index("sections", "name") 
    end 
end 

在试图创建使用轨道控制台管理用户的新用户的过程中,我得到这个对象不支持#inspect错误:

1.9.3p125 :001 > page = Page.find(1) 
    Page Load (0.7ms) SELECT `pages`.* FROM `pages` WHERE `pages`.`id` = 1 LIMIT 1 
=> #<Page id: 1, subject_id: 1, name: "First Page", permalink: "first", position: 1, visible: false, created_at: "2012-06-01 15:14:50", updated_at: "2012-06-01 15:14:50"> 
1.9.3p125 :002 > page.editors 
(Object doesn't support #inspect) 
=> 

编辑做在控制台中page.editors抛出这个错误:

1.9.3p125 :003 > page.editors 
ArgumentError: Unknown key: inverse_of 
from /Users/aditya/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/core_ext/hash/keys.rb:44:in `block in assert_valid_keys' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/core_ext/hash/keys.rb:43:in `each_key' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/core_ext/hash/keys.rb:43:in `assert_valid_keys' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/associations/builder/association.rb:33:in `validate_options' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/associations/builder/association.rb:24:in `build' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/associations/builder/collection_association.rb:23:in `build' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/autosave_association.rb:139:in `build' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/associations/builder/has_and_belongs_to_many.rb:8:in `build' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/associations/builder/collection_association.rb:13:in `build' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/associations.rb:1600:in `has_and_belongs_to_many' 
from /Users/aditya/simple_cms/app/models/admin_user.rb:4:in `<class:AdminUser>' 
from /Users/aditya/simple_cms/app/models/admin_user.rb:1:in `<top (required)>' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in `load' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in `block in load_file' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:639:in `new_constants_in' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:in `load_file' 
... 12 levels... 
from /Users/aditya/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:554:in `get' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:588:in `constantize' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:111:in `block in compute_type' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:109:in `each' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:109:in `compute_type' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:in `klass' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/associations/association.rb:117:in `klass' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/associations/association.rb:165:in `find_target?' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:332:in `load_target' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:44:in `load_target' 
from /Users/aditya/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:87:in `method_missing' 
from /Users/aditya/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start' 
from /Users/aditya/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start' 
from /Users/aditya/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>' 
from script/rails:6:in `require' 

我困在这里。我该如何解决?

+0

在AdminUser模型中试试'has_and_belongs_to_many:pages,:inverse_of => editors'我认为这应该是问题。 – abhas

+0

已修复。谢谢! :) –

+0

但我有一个新的错误出现。当我做一个AdminUser.all时,它显示一个方法错误。树桩我。 :( –

回答

1

在你的AdminUser模型中试试这个has_and_belongs_to_many :pages, :inverse_of => :editors我认为这应该是问题。