(对象不支持#inspect)卡在控制台上

时间:2012-06-06 09:30:12

标签: ruby-on-rails database ruby-on-rails-3 associations model-associations

我有三个模型,分别是Page,AdminUser和Section:

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

在尝试使用rails控制台为AdminUser创建新用户的过程中,我得到此Object不支持#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/ruby-1.9.3-p125@global/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/ruby-1.9.3-p125@global/gems/activesupport-3.2.3/lib/active_support/core_ext/hash/keys.rb:43:in `each_key'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activesupport-3.2.3/lib/active_support/core_ext/hash/keys.rb:43:in `assert_valid_keys'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/associations/builder/association.rb:33:in `validate_options'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/associations/builder/association.rb:24:in `build'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/associations/builder/collection_association.rb:23:in `build'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/autosave_association.rb:139:in `build'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/associations/builder/has_and_belongs_to_many.rb:8:in `build'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/associations/builder/collection_association.rb:13:in `build'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/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/ruby-1.9.3-p125@global/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in `load'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in `block in load_file'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:639:in `new_constants_in'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:in `load_file'
... 12 levels...
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:554:in `get'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:588:in `constantize'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:111:in `block in compute_type'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:109:in `each'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:109:in `compute_type'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:in `klass'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/associations/association.rb:117:in `klass'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/associations/association.rb:165:in `find_target?'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:332:in `load_target'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/aditya/.rvm/gems/ruby-1.9.3-p125@global/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'

我被困在这里。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

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