IN子句:rails中的条件

时间:2012-09-25 09:47:23

标签: ruby-on-rails ruby

我在rails 2中工作,我想执行Query

PunchingInformation.all(
  :select => "users.id, login, firstname, lastname, 
    sec_to_time(avg(time_to_sec(punching_informations.punch_in_time))) as 'avg_pit',
    sec_to_time(avg(time_to_sec(punching_informations.punch_out_time))) as 'avg_pot'",
  :joins => :user,
  :group => "users.id",
  :conditions => { 
    "punching_informations.date between '#{start_date}' and '#{end_date}'", 
    ["punching_informations.user_id IN (?)", employees.map { |v| v.to_i } ]
  }
)

但它总是返回错误,如

  

Mysql ::错误:'where子句'中'2012-09-01'和'2012-09-25'之间的未知列'punching_informations.date:SELECT users.id,login,firstname,lastname,sec_to_time( avg(time_to_sec(punching_informations.punch_in_time)))为'avg_pit',                 sec_to_time(avg(time_to_sec(punching_informations.punch_out_time)))为'avg_pot'FRN punching_informations INNER JOIN users ON users。id = punching_informations。user_id AND({{1 } {。{1}} ='用户'或userstype ='AnonymousUser')WHERE(userstype IN('punching_informations.user_id IN( ?)','--- \ n- 28 \ n- 90 \ n'))GROUP BY users.id

需要你的帮助。

2 个答案:

答案 0 :(得分:1)

有点不清楚你的意思(你有阵列,但是像花键一样用大括号{}),但似乎ruby将第一个字符串("punching_informations.date between '#{start_date}' and '#{end_date}'")视为一列,而第二个数组,作为期望值的数组,从而使无效的IN条件。

如果重写为

,也许可行
:conditions => { 
   [ "(punching_informations.date between '#{start_date}' AND '#{end_date}') AND punching_informations.user_id IN (?)", employees.map { |v| v.to_i } ]
 }

甚至更好

:conditions => { 
   [ "(punching_informations.date between ? AND ?) AND punching_informations.user_id IN (?)", start_date, end_date, employees.map { |v| v.to_i } ]
 }

答案 1 :(得分:-1)

在select

中添加punching_informations.date和punching_informations.user_id

:select => “punching_informations.date,punching_informations.user_id,users.id,....