Note that there are some explanatory texts on larger screens.

plurals
  1. POAttribute in Rails model appears to be nil when it's not
    text
    copied!<p>I have a very annoying and hard to figure out bug in my rails project.</p> <p>After having created a bunch of models and their relations, I want to list them.</p> <p>But I keep getting an Error "can't dup NilClass". That is, until I restart the server. Then I can list them just fine.</p> <p>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:</p> <pre> (rdb:5) self #&lt;Bar id: 1037, foo: 2237, created_at: "2009-06-09 19:52:11", updated_at: "2009-06-09 19:52:47"&gt; (rdb:5) foo TypeError Exception: can't dup NilClass (rdb:5) attributes[:foo] nil (rdb:5) attributes["foo"] 2237 </pre> <p>I doesn't matter if I reload the page. I get the same error until I restart the server.</p> <p>My model basically looks like this (the error occurs in method baz):</p> <pre><code>class FooBar &lt; ActiveRecord::Base belongs_to :foo, :class_name =&gt; "BarFoo", :foreign_key =&gt; "foo", :dependent =&gt; :destroy belongs_to :bar, :class_name =&gt; "BarFoo", :foreign_key =&gt; "bar", :dependent =&gt; :destroy validates_presence_of :foo, :on =&gt; :create def baz [foo, bar].compact end end </code></pre> <p>My schema looks like this:</p> <pre><code>create_table "foo_bar", :force =&gt; true do |t| t.integer "foo" t.integer "bar" t.datetime "created_at" t.datetime "updated_at" end </code></pre> <p><strong>Update:</strong></p> <p>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. </p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload