可能重复:
Rails - Cannot dup NilClass when using Single Table Inheritance
大家好,
我在Windows下使用Rails 2.3.8和Ruby 1.8.7(是的,我知道我应该使用linux ...现在没有时间做这个= P)
我正在尝试制作一个简单的STI模型。
我有一个继承User的类User和1个子类(BusinessContact)。
我可以调用User.new,User.create并将其保存在数据库中,但是当我尝试使用BusinessContact.new或.create时,我得到:
can't dup NilClass
D:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2219:in `dup'
D:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2219:in `scoped_methods'
D:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2223:in `current_scoped_methods'
D:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2206:in `scoped?'
D:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2474:in `send'
D:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2474:in `initialize'
D:/Users/Administrator/Projects/Addcamon/src/app/controllers/debug_controller.rb:3:in `new'
D:/Users/Administrator/Projects/Addcamon/src/app/controllers/debug_controller.rb:3:in `index'
用户迁移的创建方式如下:
# Create Users Table
create_table :users do |t|
t.string :type
# common attributes
t.string :login, :limit => 40
t.string :identity_url
t.string :name, :limit => 100, :default => '', :null => true
t.string :email, :limit => 100
t.string :crypted_password, :limit => 40
t.string :salt, :limit => 40
t.string :remember_token, :limit => 40
t.string :activation_code, :limit => 40
t.string :state, :null => :false, :default => 'passive'
t.datetime :remember_token_expires_at
t.string :password_reset_code, :default => nil
t.datetime :activated_at
t.datetime :deleted_at
t.timestamps
end
我的模型定义如下:
class User < ActiveRecord::Base
unloadable #I've added this cause I read somewhere it could fix the problem, but it didn't
include Authentication
include Authentication::ByPassword
include Authentication::ByCookieToken
include Authorization::AasmRoles
# Validations
validates_presence_of :login, :if => :not_using_openid?
validates_length_of :login, :within => 3..40, :if => :not_using_openid?
validates_uniqueness_of :login, :case_sensitive => false, :if => :not_using_openid?
validates_format_of :login, :with => RE_LOGIN_OK, :message => MSG_LOGIN_BAD, :if => :not_using_openid?
validates_format_of :name, :with => RE_NAME_OK, :message => MSG_NAME_BAD, :allow_nil => true
validates_length_of :name, :maximum => 100
validates_presence_of :email, :if => :not_using_openid?
validates_length_of :email, :within => 6..100, :if => :not_using_openid?
validates_uniqueness_of :email, :case_sensitive => false, :if => :not_using_openid?
validates_format_of :email, :with => RE_EMAIL_OK, :message => MSG_EMAIL_BAD, :if => :not_using_openid?
validates_uniqueness_of :identity_url, :unless => :not_using_openid?
validate :normalize_identity_url
end
class BusinessContact < User
end
我已经尝试了我在某处看到的无法解决的解决方案
我试过通过rails控制台,也没有运气:
user = BusinessContact.new
TypeError: can't dup NilClass
from D:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2219:in `dup'
from D:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2219:in `scoped_methods'
from D:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2223:in `current_scoped_methods'
from D:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2206:in `scoped?'
from D:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2474:in `send'
from D:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2474:in `initialize'
from (irb):9:in `new'
from (irb):9
答案 0 :(得分:-1)
您是否在BusinessContact
内定义了user.rb
班级?如果是这样,请尝试转到business_contact.rb
并查看是否有帮助。