开发者

Perform Method on all entries on the whole Ruby class (using Rails framework)

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.


RE SQL

The queries are equivalent in this case.

RE methods for collections

This what scopes are for:

scope :stats_users, lambda { |date| 
  where("employee = false AND last_sign_in_at >= ?",date)
}          
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜