0

我在 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::Error: Unknown column 'punching_informations.date between '2012-09-01' and '2012-09-25'' in 'where clause': 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' FROM punching_informations INNER JOIN usersON users.id = punching_informations.user_id AND ( users. type= 'User' OR users. type= 'AnonymousUser' ) WHERE ( punching_informations. date between '2012-09-01' and '2012-09-25'IN ('punching_informations.user_id IN (?)','--- \n- 28\n- 90\n')) GROUP BY users.id

需要你的帮助。

4

2 回答 2

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 } ]
 }
于 2012-09-25T10:15:06.217 回答
-1

在 select 中添加 punching_informations.date 和 punching_informations.user_id

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

于 2012-09-25T09:55:04.223 回答