从具有日期和时间的数据库中检索记录

时间:2009-05-07 08:40:32

标签: ruby

我在尝试从start_day和end_day相等时从数据库中检索记录时遇到问题,如果我想看到可能的记录:“从”2009年5月6日到2009年6月6日。它没有给出我是记录,并且在数据库中它只给我记录,如果类型“从”2009年5月6日到2009年5月7日。我的代码是。

  

def generate_emails

@start = "#{params[:start][:day]}-#{params[:start][:month]}-#{params[:start][:year]}"
@start_date = Date.parse(@start) rescue
if !@start_date
  render :update do |page|
    page.hide "flash-error"
    page.visual_effect :highlight, "flash-error", :duration => 3
    page.replace_html "flash-error", "Invalid date, You selected #{params[:start][:day]} days for the #{params[:start][:month]} month"
    page.show "flash-error"
   end
  return
end

@end = "#{params[:end][:day]}-#{params[:end][:month]}-#{params[:end][:year]}"
@end_date = Date.parse(@end) rescue
  if !@end_date
  render :update do |page|
      page.hide "flash-error"
      page.visual_effect :highlight, "flash-error"
      page.replace_html "flash-error", "Invalid date, You selected #{params[:end][:day]} days for the #{params[:end][:month]} month"
      page.show "flash-error"
      end
  return
 end
@sent_message = SentMessage.find(:all, :conditions => ["message_type='Email' AND estate_id=? AND sent_date >= '#{@start_date}' AND sent_date <= '#{@end_date}' OR sent_date ='#{@start_date}' AND sent_date = '#{@end_date}' ", @estate_id], :order => "sent_date DESC")

修改

至少我得到了答案..

  

def generate_emails

@start = "#{params[:start][:day]}-#{params[:start][:month]}-#{params[:start][:year]}"
@start_date = Date.parse(@start)rescue
if !@start_date
  render :update do |page|
    page.hide "flash-error"
    page.visual_effect :highlight, "flash-error", :duration => 3
    page.replace_html "flash-error", "Invalid date, You selected #{params[:start][:day]} days for the #{params[:start][:month]} month"
    page.show "flash-error"
   end
  return
end

@end = "#{params[:end][:day]}-#{params[:end][:month]}-#{params[:end][:year]}"
@end_date = Date.parse(@end) rescue
  if !@end_date
  render :update do |page|
      page.hide "flash-error"
      page.visual_effect :highlight, "flash-error"
      page.replace_html "flash-error", "Invalid date, You selected #{params[:end][:day]} days for the #{params[:end][:month]} month"
      page.show "flash-error"
      end
  return
 end

 if @start_date == @end_date
   @start_date = DateTime.parse(@start)-1
   @end_date = DateTime.parse(@end)
   @sent_message = SentMessage.find(:all, :conditions => ["message_type='Email' AND estate_id=? AND sent_date >= '#{@start_date}' AND sent_date = '#{@end_date}' ", @estate_id], :order => "sent_date DESC")
 end

@sent_message = SentMessage.find(:all, :conditions => ["message_type='Email' AND estate_id=? AND sent_date >= '#{@start_date}' AND sent_date <= '#{@end_date}' OR sent_date ='#{@start_date}' AND sent_date = '#{@end_date}' ", @estate_id], :order => "sent_date DESC")

1 个答案:

答案 0 :(得分:0)

我建议您检查Date.parse是否无法解析@start@end - 您从params获得的值不能很好由运行ruby代码的应用程序的语言环境正确解析。