Reputation: 7164
In my Rails application I have a User model (more or less built by RESTful Authentication), and several other models belonging to the user. So let's say, for example:
user has_many :posts
post belongs_to :user
Anywhere in the users resource I can access the user variables as I would expect. @user.name, @user.email, @user.login, etc. all return the expected object.
However, I can't seem to access any of these attributes from any other resource (in this example, posts) without it returning nil (nil rather than a NoMethodError).
Why is this? It seems even stranger to me that I can call current_user
just fine, as in current_user.name
, current_user.email
, but not for the user in question.
Upvotes: 0
Views: 432
Reputation:
There are two users you could be talking about here.
1) the currently logged in user
2) the user who the post belongs to
the currently logged in user should be able to be accessed through the current_user. the user who the post belongs to will need to be set in the Posts controller:
@user = Users.find(@post.user_id)
or
@user = @post.user
Upvotes: 1