Retrieving a record from a database with date and time
I am having problems trying to get a record from the database when start_day and end_day are equal, if I want to see the record, maybe "from" May 6, 2009 to May 6, 2009. It doesn't give me the record and is in the database, it only gives me the record if the type is "From" May 06, 2009 to May 7, 2009 and my code.
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")
end
EDIT
At least I got the answer.
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")
end
0
a source to share