简体   繁体   中英

Rails App search bar works locally but not on Heroku

EDIT: Not a duplicate of "Why doesn't this query work on Heroku?" . Search does not involve strings, only date queries.

Rails App throws no errors locally when searching but throws an error every time when searching on prod. This used to work fine. I'm developing against SQLite3 and haven't changed anything directly interacting with the search bar recently. I rolled back to some pretty old branches to make sure; it was still broken. I've got SQLite3 running on 1.3.9, specified in my gemfile.

Here is the search def from my model:

def self.search(search)
   where("startDay LIKE ?", "%#{search}%")
end

The view:

<%= form_tag(weeks_path, :method => "get", id: "search-form") do %>
    <h4>Search for weeks by start date </h4>
    <%= text_field_tag :search, params[:search], placeholder: "Search Weeks", value: Date.today.beginning_of_week(:sunday) - 7 %>
    <%= submit_tag "Search", :name => nil %>
<% end %>

Anything else I should include that might help solving the issue?

The main thing is not about searching for strings or dates. The problem is that your local database is different from production one. PostgreSQL can't apply LIKE operation directly on datetime fields, it should be explicitly converted to a string before using a string operation. Without switching to production type database in your development environment you will continue obtaining similar kind of bugs in future.

In postgresql this query should be written as follows:

 where("startDay::text LIKE ?", "%#{search}%")

For anyone looking at this in the future. I switched to postgreSQL, but because my app is 99% complete, I decided that rather than trying to go back through my files and change all of my syntax I would abandon Heroku, continue to use sqlite3 and instead host my own production server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM