可能重複:
Rails - Cannot dup NilClass when using Single Table Inheritance不能DUP NilClass
嗨,大家好,
我使用Rails 2.3.8和在Windows下的Ruby 1.8.7(是的,我知道我應該使用Linux ...現在沒有時間這樣做= P)
我正在嘗試製作一個簡單的STI模型。
我在哪裏有一個類User和一個繼承自User的子類(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
心中已經Ë已經嘗試過卸載的解決方案,我見過的地方
我已經通過軌道控制檯試過,沒有運氣或者:
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
它看起來像某人發佈了兩次基本相同的問題,作爲兩個不同的用戶... [Rails的 - 使用單表繼承時不能重複NilClass](http://stackoverflow.com/questions/3971496/rails-cannot- DUP-nilclass-時,使用單表繼承) – 2010-10-19 18:40:34
這是我的同事,用戶,我不知道他要問在這裏。如果這是一個問題,請刪除任何問題。 – emzero 2010-10-19 18:48:32