how to get the reference?

please help solve the problem.

There is a table users:
id :integer, autoincrement
name :string
gender_id :integer

There is a table genders:
id :integer, autoincrement
gender_name :string

model User:
class User < ActiveRecord::Base
  belongs_to :gender

model Gender:
class Gender < ActiveRecord::Base
  has_many :users

I need the gendername. I try to do so:
<% @users.each do |user| %>
    <div class="col-xs-6"><%= %></div>
    <div class="col-xs-2"><%= user.gender_id %></div>
<% end %>

the result is output number, but I need to gendername

so I tried:
<div class="col-xs-2"><%= user.gender.gender_name %></div>

in this case receives the following error message:
undefined method `gender_name' for nil:NilClass

One of your users does not have a gender associated, so user.gender is nil
try <div class="col-xs-2"><%= user.gender.gender_name if user.gender %></div>



Just wondering why you don't validate the presence/absence of gender_name on
creation of a new record...

I think it is not gender_name that is missing, but the gender object itself.