rmagnum2002
rmagnum2002

Reputation: 11421

heroku rails 3 code error in production

I've moved my app to heroku and went fine. It starts the application fine when I have no records in cars.

It starts the application fine when I have no records in cars

When I add a new car I get this error in heroku log

ActionView::Template::Error (PGError: ERROR:  invalid input syntax for integer: "t"
  SELECT  "uploads".* FROM "uploads" WHERE ("uploads".car_id = 1 AND (file_avatar = 't')) LIMIT 1):
     23:         <% if logged_in? && current_user.id == 1 -%>
     24:           <%= link_to "Select Main Photo", setmain_car_path(@car),  :remote => true, :class => 'setmain' %>
     25:         <% end %>
     26:         <% if @car.profile_avatar.nil? %>
     27:         <div id="no_picture"><%= image_tag('na.jpeg') %></div>
     28:         <% else %>
     29:         <li style="list-style:none;">
   app/views/cars/show.html.erb:26:in `_app_views_cars_show_html_erb___1259288943457494717_26795060_2985470889244562248'

in development mode on my localhost I have no such error. Any help would be appreciated.

this is the view.html code that refers to this error

<div class="latest_box">
    <div class="latest_bg"></div>
    <div class="last_added">Last added</div>
    <div class="latest_image">
      <% @cars.each do |car| %>
      <div class="latest_item">
        <% if car.profile_avatar.nil? %>
        <%= image_tag('rails.png') %>
        <% else %>
        <%=link_to image_tag(car.profile_avatar.photo.url(:thumb)), car %>
        <% end %><br />
        <b><%= link_to car.carname.name, car %></b>
        <b><%= link_to car.carmodel.name, car %></b><br />
        <%= car.category %><br />
        <%= number_to_currency(car.price) %>
      </div>
      <% end %>
    </div>

Car model

has_many :carname
  has_many :carmodel
  has_one :profile_avatar, :class_name => "Upload", :foreign_key => "car_id", :conditions => ['file_avatar = ?', true]
  has_many :uploads, :dependent => :destroy

Upvotes: 1

Views: 397

Answers (2)

mu is too short
mu is too short

Reputation: 434915

You say (in a comment) that you're developing on top of MySQL. MySQL uses integer columns for booleans with 1 for true and 0 for false. PostgreSQL has a native boolean type that accepts various things for true and false, Rails uses 't' and 'f' when working with PostgreSQL.

You have this:

:conditions => ['file_avatar = ?', true]

and that's causing your trouble. So, I'd guess that you created file_avatar as an integer column and everything worked fine in development because a Ruby true is converted to 1 by the MySQL adapter. Everything falls apart on Heroku because the PostgreSQL adapter converts true to 't' and PostgreSQL doesn't want to compare that to your integer file_avatar column.

Your best bet is to change file_avatar to a boolean column with a migration:

change_column :uploads, :file_avatar, :boolean

Then, once you have this immediate problem sorted out, change your development environment to PostgreSQL 8.3 (if you're deploying to a shared Heroku database) or PostgreSQL 9.0 (if you're deploying to a dedicated database). Developing and deploying on the same stack will save you from a lot of needless pain and suffering.

Upvotes: 3

RSG
RSG

Reputation: 7123

You're using a different local database, aren't you? Your raw SQL is not playing nicely with postgres. You need to substitute the postgres "true" for your 't'.

(file_avatar = 'true')

Upvotes: 2

Related Questions