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
end
model Gender:
class Gender < ActiveRecord::Base
has_many :users
end
I need the gendername. I try to do so:
<% @users.each do |user| %>
<%= user.name %>
<%= user.gender_id %>
<% end %>
the result is output number, but I need to gendername
p.s.
so I tried:
<%= user.gender.gender_name %>
in this case receives the following error message:
undefined method `gender_name’ for nil:NilClass
On 14 June 2015 at 08:27, Zlodiak Z. [email protected] wrote:
…
so I tried:
<%= user.gender.gender_name %>
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
<%= user.gender.gender_name if user.gender
%>
Colin
On 14 June 2015 at 18:55, Elizabeth McGurty [email protected] wrote:
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.
Colin
Just wondering why you don’t validate the presence/absence of
gender_name
on creation of a new record…
Yeah, I state corrected, but my point remains…