I am using a SQLite3 database in development, and now have started to use heroku to host my app. Heroku only supports PostgreSQL which I have modified in my GEM file
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
end
Now, everything is working as it should except when I query a date, I get this error
ActionView::Template::Error (PG::Error: ERROR: invalid input syntax for type date: ""
2012-09-12T10:03:03+00:00 app[web.1]:
2012-09-12T10:03:03+00:00 app[web.1]: LINE 1: ...-12' AND '2012-09-12' OR DATE(start_date) BETWEEN '' AND '')...
2012-09-12T10:03:03+00:00 app[web.1]: ^
2012-09-12T10:03:03+00:00 app[web.1]: 137: <% if @project_search.total_entries > 0 %>
2012-09-12T10:03:03+00:00 app[web.1]: 136:
2012-09-12T10:03:03+00:00 app[web.1]: : SELECT COUNT(*) FROM "projects" WHERE (client LIKE '%%' AND industry LIKE '%%' AND role LIKE '%%' AND tech LIKE '%%' AND business_div LIKE '%%' AND project_owner LIKE '%%' AND exception_pm LIKE '%%' AND status LIKE '%%' AND (DATE(start_date) BETWEEN '2011-09-12' AND '2012-09-12' OR DATE(start_date) BETWEEN '' AND '') AND keywords LIKE '%%')):
2012-09-12T10:03:03+00:00 app[web.1]: 138: <% if @search_performed %>
2012-09-12T10:03:03+00:00 app[web.1]: 140: <style>
2012-09-12T10:03:03+00:00 app[web.1]: 139:
Here is my project.rb, as I think this is where I need to change something:
class Project < ActiveRecord::Base
attr_accessible :edited_first_name, :edited_last_name, :first_name, :last_name, :business_div, :client, :customer_benifits, :edited_date, :end_date, :entry_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech
#validates_presence_of :business_div, :client, :customer_benifits, :end_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech
def self.search(search_client, search_industry, search_role, search_tech, search_business_div, search_project_owner, search_exception_pm, search_status, search_start_date_dd, search_start_date_A, search_start_date_B, search_keywords)
return scoped unless search_client.present? || search_industry.present? || search_role.present? || search_tech.present? || search_business_div.present? || search_project_owner.present? || search_exception_pm.present? || search_status.present? || search_start_date_dd.present? || search_start_date_A.present? || search_start_date_B.present? || search_keywords.present?
todays_date = DateTime.now.to_date
if !search_start_date_A.blank? or !search_start_date_B.blank?
search_start_date_A = Date.parse(search_start_date_A).strftime("%Y-%m-%d")
search_start_date_B = Date.parse(search_start_date_B).strftime("%Y-%m-%d")
todays_date = ""
end
if search_start_date_dd.blank?
todays_date = ""
end
if (search_start_date_A.blank? or search_start_date_B.blank?) and search_start_date_dd.blank?
where(['client LIKE ? AND industry LIKE ? AND role LIKE ? AND tech LIKE ? AND business_div LIKE ? AND project_owner LIKE ? AND exception_pm LIKE ? AND status LIKE ? AND keywords LIKE ?',
"%#{search_client}%", "%#{search_industry}%" , "%#{search_role}%" , "%#{search_tech}%" , "%#{search_business_div}%" ,
"%#{search_project_owner}%" , "%#{search_exception_pm}%" , "%#{search_status}%",
"%#{search_keywords}%"
])
else
where(['client LIKE ? AND industry LIKE ? AND role LIKE ? AND tech LIKE ? AND business_div LIKE ? AND project_owner LIKE ? AND exception_pm LIKE ? AND status LIKE ? AND (DATE(start_date) BETWEEN ? AND ? OR DATE(start_date) BETWEEN ? AND ?) AND keywords LIKE ?',
"%#{search_client}%", "%#{search_industry}%" , "%#{search_role}%" , "%#{search_tech}%" , "%#{search_business_div}%" ,
"%#{search_project_owner}%" , "%#{search_exception_pm}%" , "%#{search_status}%",
search_start_date_dd, todays_date , search_start_date_A, search_start_date_B, "%#{search_keywords}%"
])
end
end
def self.paginated_for_index(projects_per_page, current_page)
paginate(:per_page => projects_per_page, :page => current_page)
end
end
It seems SQLite3 could handle ” as an empty date, but PG can’t
Does anyone have some solution to this?
I am new to rails so please remember this when answering. Thanks in advance.
NULLand''(the empty string) are different things. Some databases, like MySQL and apparently SQLite, allow you to treat them as the same thing, but they are not.''is not a valid date. PostgreSQL won’t accept another common MySQL-ism,0000-00-00, either; watch out for that in case you’re used to using that as a placeholder or “invalid” value.You need to fix your application so that it sends
NULLfor null dates. I expect that setting the date to the application-level null/nil value (Ruby seems to usenilas its null-value) instead of to""will do the trick.