0

你能帮我解决以下问题吗?我知道这是基本的,但我似乎缺少一些非常基本的东西。当我问 SectionEdit.new 时,尽管我指向正确的类名,但事情似乎出错了。谢谢你的帮助。

这是我得到的错误。

$ edit = SectionEdit.new
NameError: undefined local variable or method `class_name' for #<Class:0x103675008>
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/dynamic_matchers.rb:50:in `method_missing'
    from /Users/seydoukonate/Sites/simple_cms/app/models/section_edit.rb:3
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in `load'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in `load_file'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:639:in `new_constants_in'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:468:in `load_file'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:353:in `require_or_load'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:502:in `load_missing_constant'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:192:in `const_missing'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `each'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `const_missing'
    from (irb):14

我已将我的模型“页面”定义如下

class SectionEdit < ActiveRecord::Base
  attr_accessible :admin_user_id, :section_id, :summary
  belongs_to :editor, class_name => "AdminUser", :foreign_key => 'admin_user_id'
  belongs_to :section
end

管理员用户

class AdminUser < ActiveRecord::Base
   attr_accessible :first_name, :last_name, :email, :username

  has_and_belongs_to_many :pages
  has_many :section_edits

  scope :named, lambda{|first,last| where(:first_name => first,:last_name => last)}
end

部分

class Section < ActiveRecord::Base
  attr_accessible :name, :position, :visible, :content_type, :content
  belongs_to :page
  has_many :section_edits
end
4

1 回答 1

6

更改class_name:class_name

belongs_to :editor, class_name => "AdminUser", :foreign_key => 'admin_user_id'

belongs_to :editor, :class_name => "AdminUser", :foreign_key => 'admin_user_id'

对于 Rails 3

belongs_to :editor, class_name: "AdminUser", foreign_key: 'admin_user_id'
于 2012-10-31T04:37:49.030 回答