Rails 模型中的属性似乎为零,但实际上并非如此
我的 Rails 项目中有一个非常烦人且难以找出的错误。
创建了一堆模型及其关系后,我想列出它们。
但我不断收到错误“无法复制 NilClass”。 也就是说,直到我重新启动服务器。 然后我就可以把它们列出来了。
调试此问题后发现,当我尝试返回其中一个属性的值时,其中一个模型的方法中会引发错误。 我在方法中有一个断点,这就是我在调试器中得到的结果:
(rdb:5) self #<Bar id: 1037, foo: 2237, created_at: "2009-06-09 19:52:11", updated_at: "2009-06-09 19:52:47"> (rdb:5) foo TypeError Exception: can't dup NilClass (rdb:5) attributes[:foo] nil (rdb:5) attributes["foo"] 2237
是否重新加载页面并不重要。 在重新启动服务器之前我收到相同的错误。
我的模型基本上看起来像这样(错误发生在方法 baz 中):
class FooBar < ActiveRecord::Base
belongs_to :foo, :class_name => "BarFoo", :foreign_key => "foo", :dependent => :destroy
belongs_to :bar, :class_name => "BarFoo", :foreign_key => "bar", :dependent => :destroy
validates_presence_of :foo, :on => :create
def baz
[foo, bar].compact
end
end
我的架构看起来像这样:
create_table "foo_bar", :force => true do |t|
t.integer "foo"
t.integer "bar"
t.datetime "created_at"
t.datetime "updated_at"
end
更新:
在我得到更多答案指出 :foo 和“foo”不一样之前:我知道它们并不相等,但这不是这里的问题。
而且,我刚刚确认 read_attribute("foo") 确实返回与 read_attribute(:foo) 相同的结果。 self[:foo] 和 self["foo"] 也是如此。 这些都不返回零。 然而,它们都返回 foo 的 id,而不是 foo 本身。
I have a very annoying and hard to figure out bug in my rails project.
After having created a bunch of models and their relations, I want to list them.
But I keep getting an Error "can't dup NilClass". That is, until I restart the server. Then I can list them just fine.
Debugging this issue, it turns out that the Error gets raised in a method in one of the models when I try to return the value of one of its attributes. I have a breakpoint in the method, and this is what I get in the debugger:
(rdb:5) self #<Bar id: 1037, foo: 2237, created_at: "2009-06-09 19:52:11", updated_at: "2009-06-09 19:52:47"> (rdb:5) foo TypeError Exception: can't dup NilClass (rdb:5) attributes[:foo] nil (rdb:5) attributes["foo"] 2237
I doesn't matter if I reload the page. I get the same error until I restart the server.
My model basically looks like this (the error occurs in method baz):
class FooBar < ActiveRecord::Base
belongs_to :foo, :class_name => "BarFoo", :foreign_key => "foo", :dependent => :destroy
belongs_to :bar, :class_name => "BarFoo", :foreign_key => "bar", :dependent => :destroy
validates_presence_of :foo, :on => :create
def baz
[foo, bar].compact
end
end
My schema looks like this:
create_table "foo_bar", :force => true do |t|
t.integer "foo"
t.integer "bar"
t.datetime "created_at"
t.datetime "updated_at"
end
Update:
Before I get any more answers pointing out that :foo and "foo" are not the same: I'm aware that they are not equal, but that is not the issue here.
And, I just confirmed that read_attribute("foo") does return the same as read_attribute(:foo). And so does self[:foo] and self["foo"]. None of these return nil. They do however all return the id of foo, instead of foo itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
终于解决了!
虽然我不太清楚为什么,但如果我在模型定义中添加“unloadable”,问题就会消失:
This site 是我找到解决方案的地方。 我完全不明白它,但它有效:-)
Finally solved it!
Although I'm not exactly sure why, the problem goes away if I add "unloadable" to the model definition:
This site is where I found the solution. I totally do not understand it, but it works :-)
:foo
不等于'foo'
。 它等于'foo'.to_sym
或'foo'.intern
。:foo
is not equal to'foo'
. It is equal to'foo'.to_sym
or'foo'.intern
.区别在于您有不同的密钥。 在 Ruby 中,:foo 与“foo”不同(:foo 是一个符号,而“foo”是一个字符串)。
您可以通过输入
:foo.to_s
来尝试一下,如果我没有记错的话,它将把符号转换为字符串。The difference is that you have a different key. In Ruby, :foo is not the same thing as "foo" (:foo is a symbol, while "foo" is a String).
You can try it out by putting
:foo.to_s
which will transform the symbol to a String if I'm not mistaken.