0

My User model has_many messages I would like to retrieve the model's unread messages as a relation so I can chain it

at the moment, the only thing that worked for me is

class Message

scope :unread, lambda{ |user| where("receiver_open = false AND received_messageable_id = ?", user.id)}

and then access it as

current_user.messages.unread(current_user)

you can see that this is a bit smelly

Is there way to make the scope use the current_user which I used to invoke the scope? so that it would be current_user.messages.unread

4

2 回答 2

0

因为,您已经在使用current_user.messages过滤消息,无需再次过滤消息。

最好在没有 lambda 的情况下编写范围

scope :unread, where("receiver_open = false")

希望,它会有所帮助。

于 2013-02-26T05:42:46.697 回答
0

即使您可以访问current_user模型中的变量:
模型也不应该知道当前谁在使用您的应用程序。

我认为一个简单的解决方案是仅使用收到的消息而不是所有消息,这样您就不必检查接收者的身份。

或者,调用您的范围unread_by将使其更易于理解且不那么臭。

于 2013-02-26T05:43:08.807 回答