对整个 Ruby 类的所有条目执行方法(使用 Rails 框架)
我正在尝试编写一个方法来选择 User 类所有成员的子集。这是我的尝试:
def self.stats_users(date)
self.where("employee = false AND last_sign_in_at >= ?", date)
end
我尝试以这种方式调用这个函数: User.stats_user('2011-04-14')
但是,这个方法正在执行这个sql语句:
SELECT "users".* FROM "users" WHERE (employee = false AND last_sign_in_at >= '2011-04-14')
当它应该简单地执行时:
SELECT * FROM "users" WHERE (employee = false AND last_sign_in_at >= '2011-04-14')
我想我真正的问题围绕着编写作用于类的所有成员的方法以及我对将这些方法放在哪里以及如何调用它们的相对无知。我似乎在理解 ActiveRecord 如何将语句转换为原始 sql 时遇到了一些困难。
I am trying to write a method that selects a subset of all members of the User class. Here is my attempt:
def self.stats_users(date)
self.where("employee = false AND last_sign_in_at >= ?", date)
end
I tried to call this function in this manner: User.stats_user('2011-04-14')
However, this method is executing this sql statement:
SELECT "users".* FROM "users" WHERE (employee = false AND last_sign_in_at >= '2011-04-14')
when it should simply execute:
SELECT * FROM "users" WHERE (employee = false AND last_sign_in_at >= '2011-04-14')
I guess my real question revolves around writing methods that act on all members of a class and my relative ignorance of where to put these methods and how to call them. I also appear to be having a little trouble understanding how ActiveRecord transforms statements into raw sql.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RE SQL
在这种情况下,查询是等效的。
集合的 RE 方法
这是 范围 的用途:
RE SQL
The queries are equivalent in this case.
RE methods for collections
This what scopes are for: