Rails One problem with table inheritance

I am trying to configure unidirectional inheritance in my Rails application for a user model and its subclasses Member, Subscriber and Staff.

I have a model file for each: user.rb, member.rb, etc.

User model defined: class User < ActiveRecord::Base; end; class User < ActiveRecord::Base; end; I have subclassed other models as such: class Member < User; end; class Member < User; end; etc.

In my users table, I have all the fields that every class needs plus a type field. Now, when I go to the console and try to create a new instance of the member or subscriber, I get the following error:

TypeError: can't dup NilClass from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2184:in 'dup' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2184:in 'scoped_methods' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2188:in 'current_scoped_methods' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2171:in 'scoped?' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2439:in 'send' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2439:in 'initialize' from (irb):6:in 'new' from (irb):6

Rails knows that subclass models exist because in the console, when I just call Member or Subscriber, I return the class definition.

I read simple documentation, but am I missing something?

+4
source share
6 answers

I tried on my side, starting from the application from scratch, and it works

Here is my user model (User.rb)

 class User < ActiveRecord::Base end 

My member model (Member.rb)

 class Member < User end 

I have one migration file to create my user table that contains:

 class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :name t.timestamps end end def self.down drop_table :users end end 

Now run the console:

 โžœ ./script/console Loading development environment (Rails 2.3.4) >> u = User.new => #<User id: nil, name: nil, created_at: nil, updated_at: nil> >> m = Member.new => #<Member id: nil, name: nil, created_at: nil, updated_at: nil> >> m.name="hop" => "hop" >> m.save => true 

However, I was not able to reproduce your error :(

+1
source

Do you have a varchar type column (row in ruby)? Try the following commands (in the new rails project)

 class Member < User end C:\projects\test\sti>ruby script\generate model user name:string type:string membertype:string exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/user.rb create test/unit/user_test.rb create test/fixtures/users.yml create db/migrate create db/migrate/20091019051506_create_users.rb C:\projects\test\sti>rake db:migrate (in C:/projects/test/sti) == CreateUsers: migrating ==================================================== -- create_table(:users) -> 0.0000s == CreateUsers: migrated (0.0000s) =========================================== C:\projects\test\sti>ruby script\console Loading development environment (Rails 2.3.4) >> u = User.new => #<User id: nil, name: nil, type: nil, membertype: nil, created_at: nil, updated_at: nil> >> m = Member.new => #<Member id: nil, name: nil, type: "Member", membertype: nil, created_at: nil, updated_at: nil> >> m.name = 'fred' => "fred" >> m.save => true >> u.name = 'rader' => "rader" >> u.save => true >> User.find :all => [#<Member id: 1, name: "fred", type: "Member", membertype: nil, created_at: "2009-10-19 05:17:11", updated_at: "2009-10-19 05:17:11">, #<User id: 2, name: "rader", type: nil, membertype: nil, created_at: "2009-10-19 05:17:24", updated_at: "2009-10-19 05:17:24">] >> 
+1
source

Check this page, there are several solutions to this problem (even in the comments).

http://strd6.com/2009/04/cant-dup-nilclass-maybe-try-unloadable/

+1
source

I think the problem is one of the definitions of your model because of the stack trace you are showing. If you still have a problem, write your code and I am sure you will get a good answer.

0
source

I had exactly this problem after I extracted some functions for the plugin.

But in my case, it worked from the console, so I made sure the id was reloaded, this line in init.rb ActiveSupport::Dependencies.load_once_paths.delete( File.expand_path(File.dirname(__FILE__))+'/app/models')

0
source

I came across something similar back and this site helped:

http://www.dansketcher.com/2009/05/11/cant-dup-nilclass/

 class User < ActiveRecord::Base unloadable ... end 

I do not know why this is happening, because I could not track anything abnormal. I really believe that this was a STI situation.

0
source

All Articles