2013-12-11 29 views
0

UserNotification belongs_to用戶和用戶has_many UserNotification。Rails - 我可以訪問控制檯中的對象,但不能訪問應用程序

我想通過用戶訪問通知。

User.find(31).user_notifications 

它返回以下用戶::

<Employer id: 31, email: "[email protected]", ...> 

上面的代碼是正確的(用戶的類型可以是「僱主」或「自由職業者」的,我相信這一點,如果我在控制檯中運行被稱爲多態)。但是,如果我運行:

User.where(email: '[email protected]').user_notifications 

,有人告訴我:

undefined method `user_notifications' 

這應該給我相同的用戶正如我上面使用「查找」方法。在我的應用程序中,我在使用current_user時遇到了與user_notification相同的問題。例如,我有:

@notifications = current_user.user_notifications.inspect 

我做這個登錄時,因爲我是在控制檯的工作相同的用戶,用戶31.然而,user_notifications顯示爲無。

如何通過current_user訪問user_notification?

+0

謝謝,這對控制檯問題的工作。這是爲什麼?我絕對只有一個用戶使用該電子郵件地址? – Philip7899

回答

1

.where返回一個數組。因此,當你使用它,你需要指定你想要的結果:

User.where(email: '[email protected]').first.user_notifications 

看到這個問題進行了詳細的解釋:Rails .where vs .find

+0

謝謝,我如何讓它在與current_user的應用程序中工作? – Philip7899

+0

我想你將不得不提供更多的代碼,比如你如何返回'current_user'?如果通過使用where來得到'current_user',則情況相同。 – RustyToms

+0

我不確定,current_user是設計人員的助手。 – Philip7899

相關問題