Rails 单表继承问题

发布于 2024-08-08 14:24:41 字数 1180 浏览 9 评论 0原文

我正在尝试在 Rails 应用程序中为用户模型及其子类成员、订阅者和员工设置单表继承。

我每个都有一个模型文件:user.rb、member.rb 等

用户模型定义为:class User class User class User class User class User class User ActiveRecord::基;结束; 我对其他模型进行了子类化,如下所示:class Member <用户;结束; 等等。

在我的用户表中,我拥有每个类所需的所有字段以及类型字段。现在,当我转到控制台并尝试创建成员或订户的新实例时,我收到以下错误:

TypeError: can't dup NilClass 来自 /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2184:in 'dup' 来自 /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2184:in 'scoped_methods' 来自 /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2188:in 'current_scoped_methods' 来自 /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2171:in 'scoped?' 来自 /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2439:in '发送' 来自 /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2439:in '初始化' 来自(irb):6:在“新”中 来自(irb):6 Rails

知道子类模型在那里,因为在控制台中,当我简单地调用 Member 或 Subscriber 时,我会得到返回的类定义。

我已经阅读了简单的文档,但我一定错过了一些东西?

I'm trying to setup single table inheritance in my Rails app for a User model and its subclasses Member, Subscriber, and Staff.

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

The user model is defined: class User < ActiveRecord::Base; end;
I subclassed the other models as such: class Member < User; end; and so on.

In my users table I have all the fields every class needs plus the type field. Now when I go to the console and try to create a new instance of say 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 know the subclasses models are there because in the console when I simply call Member or Subscriber, i get the class definition returned.

I've read the simple documentation, but I must be missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

枫林﹌晚霞¤ 2024-08-15 14:24:41

我尝试从头开始应用程序,它可以工作

这是我的用户模型(User.rb)

class User < ActiveRecord::Base
end

我的成员模型(Member.rb)

class Member < User
end

我有一个迁移文件来创建我的用户表,其中包含:

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

现在启动控制台:

➜  ./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

但是我没能重现你的错误:(

I tried on my side starting from a scratch application 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 users table which 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 launching 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 did not manage to reproduce your error :(

意犹 2024-08-15 14:24:41

您是否有 varchar 类型(ruby 中的字符串)的类型列?尝试以下命令(在新的 Rails 项目中)

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">]
>>

Do you have a type column of type varchar (string in ruby)? Try the following commands (in a 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">]
>>
生死何惧 2024-08-15 14:24:41

检查这个页面,这个问题的解决方案不止几个(甚至在评论中)。

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

Check this page, there are more than few solutions to this problem (even in comments).

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

盛夏已如深秋| 2024-08-15 14:24:41

由于您显示的堆栈跟踪,我认为问题出在您的模型定义之一中。如果您仍然遇到问题,请粘贴您的代码,我相信您会得到一个很好的答案。

I'm thinking that the problem is in one of your model definitions because of the stack trace you show. If you still are having a problem, pastie your code, and i'm sure you'll get a good answer.

护你周全 2024-08-15 14:24:41

在我将一些功能提取到插件中之后,我就遇到了这个问题。

但我的情况是它在控制台工作,所以我确保重新加载了 id,并在 init.rb 中添加了这一行
ActiveSupport::Dependency.load_once_paths.delete(
File.expand_path(File.dirname(__FILE__))+'/app/models')

I hade exactly this problem, after I extracted some functionality to a plugin.

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

我做我的改变 2024-08-15 14:24:41

我不久前遇到了类似的事情,这个网站提供了帮助:

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

class User < ActiveRecord::Base
  unloadable

  ...
end

不知道为什么会发生这种情况,因为我无法追踪任何异常情况。但我确实相信这是性传播感染的情况。

I ran into something similar a while back and this website helped:

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

class User < ActiveRecord::Base
  unloadable

  ...
end

Not sure why this occurs as I could not track down anything abnormal. I do believe it was a STI situation though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文