获取与复杂连接相关的模型的行数
我是 Rails 新手,不确定执行此操作的最佳方法。我有一个“有很多”事件的用户。我希望能够进入 user.active?在一个视图中,该视图将显示用户在过去 5 分钟内是否创建了一个或多个关联事件。
我知道语法不正确,但这就是我到目前为止所得到的:
def active?
find(:conditions => {:events => ["created_at >= ?", DateTime.now - 5.minute]}).count > 0
end
I'm new to rails and am unsure of the best way of doing this. I have a User which "has many" Events. I want to be able to go user.active? in a view which will display whether the user has one or more associated Events created in the last 5 minutes.
I know the syntax isn't correct but this is what I've got so far:
def active?
find(:conditions => {:events => ["created_at >= ?", DateTime.now - 5.minute]}).count > 0
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在事件集合上查找:
You can just do a find on the events collection: