尝试创建自日期x以来的记录的Rails模型范围查询,使用Lambda吗? [英] Trying to create a rails model scope query for records since date x, use a lambda?

查看:74
本文介绍了尝试创建自日期x以来的记录的Rails模型范围查询,使用Lambda吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我想将模型级查找器与lambda结合使用.

I think I will want to use combine a model level finder with a lambda.

named_scope :recent_snaps, lambda {|since_when| {:conditions=>{:created_at >= since_when}}}

但是我不确定语法是否正确,尤其是参数部分,并且现在无法运行应用程序以在控制台进行检查.

but I am not sure and also if I have the syntax correct, espcially for the parameter piece and can't run the app right now to check at console.

我不想使用find_by_sql或控制器查找,而是希望我的查找器在模型级别进行rspec测试.

I do not want to use a find_by_sql or a controller find, I want my finder at the model level for rspec testing.

推荐答案

如果您使用的是Rails 3(如您所愿,给定问题的标签),则应使用scope而不是named_scope而不是conditions.此外,您不能在哈希中使用>=.

If you're on Rails 3 (as you presumably are, given the question's tags) you should be using scope rather than named_scope and where rather than conditions. Additionally, you can't use >= in a hash.

您完成的范围应如下所示:

Your finished scope should look something like this:

scope :recent_snaps, lambda { |since_when| where("created_at >= ?", since_when) }

这篇关于尝试创建自日期x以来的记录的Rails模型范围查询,使用Lambda吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆