Ruby on Rails는 모든 시간을 UTC (created_at 및 updated_at 필드)에 저장하고 데이터베이스에서 활성 레코드 개체를 가져올 때 RoR에 날짜, (환경 .rb에서) 구성된 시간대를 변환하여 표시합니다.created_at 필터를 기반으로 데이터베이스에서 활성 레코드 개체 쿼리하기
하지만 내 경우가 다릅니다. 나는 사용자 지정 쿼리를 작성하고 있습니다. 그리고 where 절을 수동으로 추가하고 있습니다. Where 절은 다음과 같습니다. select * where created_at > [user entered date].
이제는 사용자가 입력 한 날짜가 UTC - 7이며 created_at은 UTC로되어 있습니다. 그래서 나는 그것을 실제로 작동시키지 못합니다. 난 select * where created_at > [user-entered-date] 07:00:00
같은 하드 코드 수 있습니다 -하지만 일광 절약 시간 때문에이 문제를 만들었으며 또한 좋은 해결책처럼 보이지 않습니다.
두 번째 문제는 record.created_at를 인쇄 할 때 UTC 날짜를 얻는 것입니다 (아마도 사용자 정의 쿼리를 작성했기 때문일 수도 있습니다). 또한 수동으로 원하지 않습니다. (하드 코드) 현지 시간으로 변환합니다.
cond = EZ::Where::Condition.new
if !start_date.empty?
start_date = params[:filter][:start_date].to_date.to_s(:db)
cond.append "(registrations.created_at) >= '#{start_date} 07:00:00'" #Here!
end
if !end_date.empty?
end_date = params[:filter][:end_date].to_date
end_date = end_date + 1.day;
end_date = end_date.to_s(:db)
cond.append "(registrations.created_at) <= '#{end_date} 07:00:00'" #Here!
end
registrations = Registration.all(
:joins => [:event],
:select => 'registrations.id, registrations.first_name, registrations.last_name, registrations.company_name,
(registrations.created_at) AS reg_date, events.name AS evt_name, sum(fees) AS fees, code, events.id AS event_id',
:group => 'registrations.id',
:order => 'evt_name, events.id',
:conditions=> cond.to_sql
)
unless registrations.empty?
registrations.each_with_index do |registration, i|
sheet[ i, 3 ] = (DateTime.strptime(registration.reg_date, "%Y-%m-%d %H:%M:%S") - 7.hours).to_date #Here!
end
end
을 사용하기 :
다음은 쿼리에 대한 내 코드입니다 내 자신을 교육하는 동안보고, 그래서 지금은이 일에 붙어 있습니다. –