Note that there are some explanatory texts on larger screens.

plurals
  1. POBelongs_to only recognized by console, not server
    text
    copied!<p>I've got a really weird problem with my project. I've got 2 models, the one is Link and the other Category. I've got a index view where all the links should be listed, together with the corresponding category names. When running the server and trying to use</p> <pre><code>&lt;%= link.category.name %&gt; </code></pre> <p>I get an error page with the following:</p> <pre><code>undefined method `name' for nil:NilClass </code></pre> <p>But when I open the console and write:</p> <pre><code>link = Link.find(1) #there is currently only one link link.category.name </code></pre> <p>It returns the correct category name.</p> <p>Here are my Models and schema.rb:</p> <pre><code>class Link &lt; ActiveRecord::Base attr_accessible :category_id, :description, :title, :url, :visible belongs_to :category scope :visible, lambda { where(visible: true) } end </code></pre> <p>.</p> <pre><code>class Category &lt; ActiveRecord::Base attr_accessible :name has_many :links end </code></pre> <p>.</p> <pre><code>ActiveRecord::Schema.define(:version =&gt; 20130420070717) do create_table "categories", :force =&gt; true do |t| t.string "name" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end add_index "categories", ["id"], :name =&gt; "index_categories_on_id" create_table "links", :force =&gt; true do |t| t.string "title" t.text "description" t.string "url" t.integer "category_id" t.boolean "visible" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end add_index "links", ["category_id"], :name =&gt; "index_links_on_category_id" add_index "links", ["id"], :name =&gt; "index_links_on_id" end </code></pre> <p>How can this happen? Thank you very much for your help!</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