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

  • Home
  • SEARCH
  • 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 6758129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:44:29+00:00 2026-05-26T13:44:29+00:00

Schema: create_table reports, :force => true do |t| t.integer user_id t.string apparatus t.string capt

  • 0

Schema:

  create_table "reports", :force => true do |t|
    t.integer  "user_id"
    t.string   "apparatus"
    t.string   "capt"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", :force => true do |t|
    t.string   "login",                     :limit => 40
    t.string   "name",                      :limit => 100, :default => ""
    t.string   "email",                     :limit => 100
    t.string   "crypted_password",          :limit => 40
    t.string   "salt",                      :limit => 40
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "remember_token",            :limit => 40
    t.datetime "remember_token_expires_at"
    t.string   "rank"
    t.integer  "shift"
    t.integer  "access"
  end

user model:

Class User < ActiveRecord::Base
    has_many :reports
#   bunch of other stuff thats not important
end

report model:

Class Report < ActiveRecord::Base
    belongs_to :user
end

views/reports/index

<% @reports.each do |report| %>
  <tr>
    <td><%= report.user_id %></td>  #     ****THIS IS THE LINE IN QUESTION****
    <td><%= report.apparatus %></td>
    <td><%= report.capt %></td>
    <td><%= report.body %></td>
    <td><%= link_to 'Show', report %></td>
    <td><%= link_to 'Edit', edit_report_path(report) %></td>
    <td><%= link_to 'Destroy', report, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>

I would like to be able to display the name of the user that created the report. I was under the assumption that declaring the belongs_to and has_many associations would make this possible by writing report.user.name or something like that. Where have I gone wrong?

  • 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-05-26T13:44:30+00:00Added an answer on May 26, 2026 at 1:44 pm

    I’m 99% sure it’s because one or more of your reports do not have an associated user. Try

    <%= report.user.name rescue "none" %>
    

    When there is no value in user_id field on a report then report.user will return nil. So report.user.name would be like calling nil.name, which raises an error.

    UPDATE: here’s a better way:

    <%= report.user.try(:name) %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following: Schema create_table mouldings, :force => true do |t| t.string suppliers_code
I have the following table schema; CREATE TABLE `db1`.`sms_queue` ( `Id` INTEGER UNSIGNED NOT
Given the following schema: CREATE TABLE players ( id BIGINT PRIMARY KEY, name TEXT
In SQLite, given this database schema CREATE TABLE observations ( src TEXT, dest TEXT,
I have the next database schema: CREATE TABLE A (id INTEGER); CREATE TABLE Single_Values
Assuming I have an SQL table with this schema: CREATE TABLE( foo INTEGER, bar
Say I have a table with the following schema: CREATE TABLE tasks (taskID TEXT,
I've a table [File] that has the following schema CREATE TABLE [dbo].[File] ( [FileID]
I have a table in PostgreSQL where the schema looks like this: CREATE TABLE
I have an orders table with a schema like this. CREATE TABLE orders (

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.