Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9146611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:47:25+00:00 2026-06-17T10:47:25+00:00

today my error is Called id for nil, which would mistakenly be 4 —

  • 0

today my error is Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id.

I’ve got models & controllers for: User, Ticket, Answers defined in such way:

User has many Answers & Tickets

Ticket has many Answers

Answer belongs to User & belongs to Ticket

The error appears when in Ticket view I’m trying to display Answers author (User) doing it like this:

Ticket Load (0.2ms) SELECT "tickets".* FROM "tickets" LIMIT 1
=> #<Ticket id: 8, topic: "This is sample question", message: "This is Sample question with <h1>Some</h1> HTML! <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>

Answer Load (0.2ms) SELECT "answers".* FROM "answers" LIMIT 1
=> #<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">

User Load (0.2ms) SELECT "users".* FROM "users" LIMIT 1
=> #<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>

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
 => [#<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">] 

When I try to display answer a.user.username it returns error:

undefined method username for nil:NilClass

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
@ticket.user.username

Models:

class Ticket < ActiveRecord::Base
  attr_accessible :topic, :message, :user, :category, :category_id
  has_many :answers
  belongs_to :user
  belongs_to :category

end

class Answer < ActiveRecord::Base
  attr_accessible :ticket, :user, :message
  belongs_to :user
  belongs_to :ticket, :counter_cache => true
end

class User < ActiveRecord::Base
  attr_accessible :username, :email, :password, :password_confirmation
  has_many :tickets
  has_many :answers
end

Controller’s partials:

class TicketsController < ApplicationController
  before_filter :login_required
  before_filter :admin_required, :only => [:index, :show]
  def index
    @tickets = Ticket.all
  end

  def show
    @ticket = Ticket.find(params[:id])
  end
end

class AnswersController < ApplicationController
  def create
    t_attr = params[:answer].merge :user => current_user
    @ticket = Ticket.find(params[:ticket_id])
    @answer = @ticket.answers.create(t_attr)
    redirect_to ticket_path(@ticket)
  end
end

Edit:
Through console I can obtain answer’s author userdata:

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
 => #<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> 

Ticket view:

<span class="pull-right">by  <strong><%=h @ticket.user.username %></strong> on  <i><%=h @ticket.created_at %> in <strong><%= @ticket.category.name %></strong></i></span>



<p>
  <strong>Message:</strong><br>
  <%=h @ticket.message %> 
</p>
<h3>Replies</h3>
<%= form_for([@ticket, @ticket.answers.build]) do |f| %>
    <%= f.label :message %> <br>
    <%= f.text_area :message, :cols => "50", :rows => "6", :class=> "span12" %><br>
    <%= f.submit %>
<% end %>
<% @ticket.answers.each do |a| %>
    <%= h a.message %> <%= a.user.username %> <%= a.created_at %>
<% end %>
</div>

Workaround:

    <% @ticket.answers.each do |a| %>
    <%= h a.message %> <%= a.user.username if a.user %> <%= a.created_at %>
<% end %>

With this condition everything works.
Seems like Loop is doing 1 extra iteration, even if there’s no answers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:47:27+00:00Added an answer on June 17, 2026 at 10:47 am

    Rails scaffolding creates new, empty object for this:

    <%= form_for([@ticket, @ticket.answers.build]) do |f| %>
    

    to perform form operations like displaying entered form inputs after validation is unsuccesfull (so user won’t loose data entered into form fields),
    as specified in answers controller:

      def create
        t_attr = params[:answer].merge :user => current_user
        @ticket = Ticket.find(params[:ticket_id])
        @answer = @ticket.answers.create(t_attr)
        redirect_to ticket_path(@ticket)
      end
    

    Because of this loop was doing 1 extra iteration (with one object at the end).

    To fix it I simply iterate over ticket.answers.length - 1 objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I've got a stacktrace with a very strange error. Actually, I may be
Today I got a project written in PHP. There's an error when I try
Today I was working on a project in which I wanted to alias an
Today I had to work with a remote branch called origin/}__test_syntax_error_in_simpack_settings . I wanted
I've encountered a strage error today. I have an NSArray and an NSMutableDictionary .
I just started getting this error today, seemingly out of nowhere. Any one see
today I ran into an error and have no clue how to fix it.
Today, I have made an error that frustrated me for hours to see what
Out of the blue today I started getting this error in Visual Studio 2008
Hi i have a debian server. Today my site was showing Error establishing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.