Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to obtain author of answer in nested model gives `Called id for nil, which would mistakenly be 4`
    primarykey
    data
    text
    <p>today my error is <code>Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id</code>.</p> <p>I've got models &amp; controllers for: User, Ticket, Answers defined in such way:</p> <p><code>User</code> has many <code>Answers</code> &amp; <code>Tickets</code></p> <p><code>Ticket</code> has many <code>Answers</code> </p> <p><code>Answer</code> belongs to <code>User</code> &amp; belongs to <code>Ticket</code></p> <p>The error appears when in <code>Ticket</code> view I'm trying to display <code>Answers</code> author (<code>User</code>) doing it like this:</p> <p><code>Ticket Load (0.2ms) SELECT "tickets".* FROM "tickets" LIMIT 1 =&gt; #&lt;Ticket id: 8, topic: "This is sample question", message: "This is Sample question with &lt;h1&gt;Some&lt;/h1&gt; HTML! &lt;p...", user_id: 1, category_id: 2, created_at: "2013-01-15 12:24:20", updated_at: "2013-01-15 12:24:20", answers_count: 1&gt;</code></p> <p><code>Answer Load (0.2ms) SELECT "answers".* FROM "answers" LIMIT 1 =&gt; #&lt;Answer id: 6, ticket_id: 8, user_id: 1, message: "This is answer to the sample question with use of h...", created_at: "2013-01-15 12:26:46", updated_at: "2013-01-15 12:26:46"&gt;</code></p> <p><code>User Load (0.2ms) SELECT "users".* FROM "users" LIMIT 1 =&gt; #&lt;User id: 1, username: "mickula", email: "xx@xx.com", password_hash: "$2a$10$Hty2ekM3t8Dqf0CvZm5zEOwVnXAoytimW9tIOxtfjwNG...", password_salt: "$2a$10$Hty2ekM3t8Dqf0CvZm5zEO", created_at: "2013-01-12 16:55:00", updated_at: "2013-01-12 16:55:00", permission_level: nil&gt;</code></p> <pre><code>User.first.answers User Load (0.2ms) SELECT "users".* FROM "users" LIMIT 1 Answer Load (0.1ms) SELECT "answers".* FROM "answers" WHERE "answers"."user_id" = 1 =&gt; [#&lt;Answer id: 6, ticket_id: 8, user_id: 1, message: "This is answer to the sample question with use of h...", created_at: "2013-01-15 12:26:46", updated_at: "2013-01-15 12:26:46"&gt;] </code></pre> <p>When I try to display answer <code>a.user.username</code> it returns error:</p> <p><code>undefined method username for nil:NilClass</code></p> <p>So it seems like that answer is not connected with user. Could you point me where I did mistake and why it behaves like that? I would like to mention that I did it this way for Tickets and there I can display author of ticket with <code>@ticket.user.username</code> </p> <p>Models:</p> <pre><code>class Ticket &lt; ActiveRecord::Base attr_accessible :topic, :message, :user, :category, :category_id has_many :answers belongs_to :user belongs_to :category end class Answer &lt; ActiveRecord::Base attr_accessible :ticket, :user, :message belongs_to :user belongs_to :ticket, :counter_cache =&gt; true end class User &lt; ActiveRecord::Base attr_accessible :username, :email, :password, :password_confirmation has_many :tickets has_many :answers end </code></pre> <p>Controller's partials:</p> <pre><code>class TicketsController &lt; ApplicationController before_filter :login_required before_filter :admin_required, :only =&gt; [:index, :show] def index @tickets = Ticket.all end def show @ticket = Ticket.find(params[:id]) end end class AnswersController &lt; ApplicationController def create t_attr = params[:answer].merge :user =&gt; current_user @ticket = Ticket.find(params[:ticket_id]) @answer = @ticket.answers.create(t_attr) redirect_to ticket_path(@ticket) end end </code></pre> <p>Edit: Through console I can obtain answer's author userdata:</p> <pre><code>Ticket.first.answers.first.user Ticket Load (0.2ms) SELECT "tickets".* FROM "tickets" LIMIT 1 Answer Load (0.1ms) SELECT "answers".* FROM "answers" WHERE "answers"."ticket_id" = 8 LIMIT 1 User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 =&gt; #&lt;User id: 1, username: "mickula", email: "xx@xx.com", password_hash: "$2a$10$Hty2ekM3t8Dqf0CvZm5zEOwVnXAoytimW9tIOxtfjwNG...", password_salt: "$2a$10$Hty2ekM3t8Dqf0CvZm5zEO", created_at: "2013-01-12 16:55:00", updated_at: "2013-01-12 16:55:00", permission_level: nil&gt; </code></pre> <p>Ticket view:</p> <pre><code>&lt;span class="pull-right"&gt;by &lt;strong&gt;&lt;%=h @ticket.user.username %&gt;&lt;/strong&gt; on &lt;i&gt;&lt;%=h @ticket.created_at %&gt; in &lt;strong&gt;&lt;%= @ticket.category.name %&gt;&lt;/strong&gt;&lt;/i&gt;&lt;/span&gt; &lt;p&gt; &lt;strong&gt;Message:&lt;/strong&gt;&lt;br&gt; &lt;%=h @ticket.message %&gt; &lt;/p&gt; &lt;h3&gt;Replies&lt;/h3&gt; &lt;%= form_for([@ticket, @ticket.answers.build]) do |f| %&gt; &lt;%= f.label :message %&gt; &lt;br&gt; &lt;%= f.text_area :message, :cols =&gt; "50", :rows =&gt; "6", :class=&gt; "span12" %&gt;&lt;br&gt; &lt;%= f.submit %&gt; &lt;% end %&gt; &lt;% @ticket.answers.each do |a| %&gt; &lt;%= h a.message %&gt; &lt;%= a.user.username %&gt; &lt;%= a.created_at %&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p>Workaround:</p> <pre><code> &lt;% @ticket.answers.each do |a| %&gt; &lt;%= h a.message %&gt; &lt;%= a.user.username if a.user %&gt; &lt;%= a.created_at %&gt; &lt;% end %&gt; </code></pre> <p>With this condition everything works. Seems like Loop is doing 1 extra iteration, even if there's no answers.</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