Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 object.belongs_to_id to object.belongs_to.name?
    primarykey
    data
    text
    <p>I have a rails 3 project where the index.html.erb currently includes <code>&lt;td&gt;&lt;%= chapter.university_id %&gt;&lt;/td&gt;</code> whereas what I want it to show should be <code>&lt;td&gt;&lt;%= chapter.university.name %&gt;&lt;/td&gt;</code> however, this is currently throwing the exception <strong>undefined method `name' for nil:NilClass</strong> Interestingly, chapter.foo.name is working to access information from another model to which "chapter" belongs. Can anyone help standardize this?</p> <p><strong><em>EDIT:</em></strong></p> <p>In an attempt to replicate this error in another way, I changed <code>&lt;td&gt;&lt;%= chapter.university.name %&gt;&lt;/td&gt;</code> to <code>&lt;td&gt;&lt;%= chapter.university.id %&gt;&lt;/td&gt;</code>, hoping that I could convince rails to give me university_id from the universities table, rather than the chapters table. Still no luck, but gave the error <strong>Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id</strong> rather than <strong>undefined method `name' for nil:NilClass</strong> which I had previously been receiving. </p> <p>I have also checked and re-checked the 'has_many'/'belongs_to' relationships between <code>chapter</code> and <code>university</code> as well as <code>chapter</code> and <code>foo</code> and found the working and non-working relationships to be identical. I have even gone so far as to switch the order in which those relationships are declared within the models, to no effect.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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