Reputation: 4677
UserNotification belongs_to User and User has_many UserNotification.
I am trying to access the notifications through the user. If I run in the console:
User.find(31).user_notifications
It returns the following user:
<Employer id: 31, email: "[email protected]", ...>
The code above is correct (A user can be of type 'employer' or 'freelancer'. I believe this is called polymorphic). However, if I run:
User.where(email: '[email protected]').user_notifications
I am told that:
undefined method `user_notifications'
This should give me the same user as the 'find' method I use above. In my application, I am having the same problem referring to the user_notification when using current_user. For example, I have:
@notifications = current_user.user_notifications.inspect
I do this while logged in as the same user I was working with in the console, User 31. However, the user_notifications are displayed as nil.
How do I access user_notification through current_user?
Upvotes: 0
Views: 56
Reputation: 7810
.where
returns an array. So when you use it you need to specify which result you want:
User.where(email: '[email protected]').first.user_notifications
See this question for a detailed explanation:Rails .where vs .find
Upvotes: 1