Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在ruby上获取在一周中特定日期创建的用户?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何在ruby上获取在一周中特定日期创建的用户?

Ruby on rails 如何在ruby上获取在一周中特定日期创建的用户?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在尝试在RubyonRails上创建一些kpi 例如,如何计算周日或周三创建的所有用户 我现在知道ruby可以通过以下代码获得一周中的一天,例如: u = User.last u.created_at.wday #==> wday get the day of week (0: sunday, 1: monday..) 但以下方法不起作用: User.where(created_at.wday: 0).count 我不想循环每个用户,检查它是否是在周日创建的,并将其放入一个数组中,

我正在尝试在RubyonRails上创建一些kpi

例如,如何计算周日或周三创建的所有用户

我现在知道ruby可以通过以下代码获得一周中的一天,例如:

u = User.last
u.created_at.wday #==> wday get the day of week (0: sunday, 1: monday..)
但以下方法不起作用:

User.where(created_at.wday: 0).count
我不想循环每个用户,检查它是否是在周日创建的,并将其放入一个数组中,因为它似乎很昂贵


有什么想法吗?

如果您使用的是MYSQL,您可以使用

对于博士后来说,这应该可以做到:

User.where("extract(dow from created_at) = ?", 0)
编辑:对于SQLITE,以下工作:

User.where("strftime('%w', created_at) = ?", 0)

如果您使用的是MYSQL,那么您可以使用

对于博士后来说,这应该可以做到:

User.where("extract(dow from created_at) = ?", 0)
编辑:对于SQLITE,以下工作:

User.where("strftime('%w', created_at) = ?", 0)