Hedgar Bezerra
Hedgar Bezerra

Reputation: 51

Rails Error querying data from Postgres on Heroku

I've just deployed my first app on Heroku with Postgres and I'm getting an error when querying data to the models. It's been working properly with local MySQL database when the app queries this through the controller:

SELECT "locations".* 
FROM "locations" 
WHERE (id LIKE '%1%' OR user_id LIKE '%1%')):

 18:   </thead>
 19: 
 20:   <tbody>
 21:     <% @locations.each do |location| %>
 22:       <tr>
 23:         <td><%= location.id %></td>
 24:         <td><%= location.user.name %></td>
 FATAL -- : [e8968d66-5740-4005-9236-fd3e4cc61542] app/views/locations/index.html.erb:21:in `_app_views_locations_index_html_erb___341067609742207438_69941629868780'

This is the method that does the search:

     def index
    if  params[:search].present?
      @search = params[:search]
      @locations = Location.where('id LIKE ? OR user_id LIKE ?', "%#{@search}%", "%#{@search}%") 
    else 
       @locations = Location.all
    end
  end

Upvotes: 0

Views: 26

Answers (1)

Nitin Srivastava
Nitin Srivastava

Reputation: 1424

Here are some confusion. You mentioned Postgres on Heroku in title while MySql in description.

I assumed you are using Postgres to write below code.

In case if you are using MySql then use LIKE in given code.

@locations = Location.where("id ILIKE :search OR user_id ILIKE :search", search: "%#{@search}%")

Let me know if you find this helpful!!!

Upvotes: 1

Related Questions