I don't understand why I can't navigate through my models with activerecord.
I have a User model that has_one profile (actually, the details of a user)
class User < ActiveRecord::Base
has_one :profile, :dependent => :destroy
end
A model Profile that belongs_to User and City
class Profile < ActiveRecord::Base
belongs_to :user
belongs_to :city
end
And a model City that has_many Profiles
class City < ActiveRecord::Base
belongs_to :country
has_many :profiles
end
In my user_controller, I can access the profile like this:
@user = User.find(params[:id])
logger.info(@user.profile.inspect)
But I can't go deeper like this:
@user = User.find(params[:id])
logger.info(@user.profile.city.inspect)
Returns
undefined method `city' for nil:NilClass
What I want to get is the name of the City from the city_id stored in the Profile model. Does someone can explain to me what I do wrong? Thanks
@user.profile.inspect
? I think it isnil
. – Rahul Tapaliundefined method
city' for #<Profile:0x00000003ef3870>` – Gozup