Rails 单表继承问题
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我尝试从头开始应用程序,它可以工作
这是我的用户模型(User.rb)
我的成员模型(Member.rb)
我有一个迁移文件来创建我的用户表,其中包含:
现在启动控制台:
但是我没能重现你的错误:(
I tried on my side starting from a scratch application and it works
Here is my User model (User.rb)
My member model (Member.rb)
I have one migration file to create my users table which contains:
Now launching the console:
However I did not manage to reproduce your error :(
您是否有 varchar 类型(ruby 中的字符串)的类型列?尝试以下命令(在新的 Rails 项目中)
Do you have a type column of type varchar (string in ruby)? Try the following commands (in a new rails project)
检查这个页面,这个问题的解决方案不止几个(甚至在评论中)。
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/
由于您显示的堆栈跟踪,我认为问题出在您的模型定义之一中。如果您仍然遇到问题,请粘贴您的代码,我相信您会得到一个很好的答案。
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.
在我将一些功能提取到插件中之后,我就遇到了这个问题。
但我的情况是它在控制台工作,所以我确保重新加载了 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')
我不久前遇到了类似的事情,这个网站提供了帮助:
http: //www.dansketcher.com/2009/05/11/cant-dup-nilclass/
不知道为什么会发生这种情况,因为我无法追踪任何异常情况。但我确实相信这是性传播感染的情况。
I ran into something similar a while back and this website helped:
http://www.dansketcher.com/2009/05/11/cant-dup-nilclass/
Not sure why this occurs as I could not track down anything abnormal. I do believe it was a STI situation though.