views:

30

answers:

1

Possible Duplicate:
Rails - Cannot dup NilClass when using Single Table Inheritance

Hi guys,

I'm using Rails 2.3.8 and Ruby 1.8.7 under Windows (yes I know I should be using linux... don't have time to do that now =P)

I'm trying to make a simple STI model.

Where I have a class User and 1 subclass (BusinessContact) inheriting from User.

I can call User.new, User.create and it does get saved in the DB, but when I try BusinessContact.new or .create I get:

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'

The User migration is created as follows:

# 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

And my models are defined like this:

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

I've already tried the unloadable solution I've seen somewhere

I've tried via rails console, no luck either:

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
A: 

Are you defining the BusinessContact class inside user.rb? If so, try moving to business_contact.rb and see if that helps.

cdmwebs
No, BusinessConact is defined as a new rails model
emzero