您可以帮助我解决以下问题吗?我知道这是基本的,但我似乎缺少一些非常基本的东西。当我问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
答案 0 :(得分: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'