0

Rails 2で作業していますが、クエリを実行したい

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:不明な列'punching_informations.date between' 2012-09-01'and' 2012-09-25''in' whereclause':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(userstype=' User'OR userstype='AnonymousUser' )WHERE(.IN('punching_informations.user_id IN(?)'、'--- \ n- 28 \ n- 90 \ n'))GROUP BY punching_informationsusers.iddate between '2012-09-01' and '2012-09-25'

君の力が必要。

4

2 に答える 2

1

意味は少しわかりませんが(配列はありますが、中かっこ{}をハッシュのように取り込んでいます)、rubyは最初の文字列("punching_informations.date between '#{start_date}' and '#{end_date}'")を列として扱い、2番目の配列を期待値の配列として扱うようです。無効な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 に答える