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 に答える