I have a API developed in Rails 4.2.5 and in this problem, I try to show all users, but each user have a relation one to one, for example one user have one profile, and when I try to access, for example to name that exist in the relation, I can not, This is mi code:
index.json.jbuilder
json.users @users do |user|
json.type user.class.name.underscore.pluralize
json.id user.id
json.attributes do |json|
json.name user.name
json.email user.email
end
json.profile do
json.gender user.profile.gender
end
end
And when I access to relation directly, with this code it works, I can view all information from the profile
json.profile do
json.gender user.profile
end
But when I try to access to other properties in specific, for example:
json.profile do
json.gender user.profile.gender
end
I can not, only shows me this error:
undefined method `gender' for nil:NilClass
json.profile do
json.gender user.profile.gender
end
Models:
user.rb
class User < ActiveRecord::Base
has_one :profile
end
profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
end
Thanks :)