0

我有三个模型,每个模型都是这样连接的:Groupswhich has many Studentswhich has many Absences.

Absences有一个名为created_on.

我只有一个组 id,想获取今天缺席的所有学生。

我在我的Student模型中创建了这个方法:

# Inside student.rb

def self.absent_today_in_group (group)
 #SQLITE
 find(:all, :joins => :absences, :conditions => ["STRFTIME('%d', created_on) = ? AND STRFTIME('%m', created_on) = ?", Date.today.day, Date.today.month])

 #POSTGRES
 #find(:all, :joins => :absences, :conditions => ["EXTRACT(DAY FROM created_on) = ? AND EXTRACT(MONTH FROM created_on) = ?", Date.today.day, Date.today.month])
end

为什么该查询不返回任何内容?然后我怎么能检查group_id?

4

2 回答 2

3

您使用的是什么版本的导轨?您可以在 Rails 3 中执行此操作:

def self.absent_today_in_group(group)
  joins(:absences, :group).where(
    'absences.created_on' => (Time.now.beginning_of_day..Time.now.end_of_day),
    'groups.id' => group.id
  )
end

这将找到给定组今天缺席的所有用户。

于 2012-09-13T13:47:37.240 回答
0

这不应该是 :where 而不是 :conditions 吗?

于 2012-09-13T13:34:32.350 回答