I have an app I am deploying to Heroku. Everything seems to work besides the “show” action for my User model.
Here is my code for the user model (what’s relevant, anyway)
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :username, :email, :password,
:password_confirmation, :confirmed,
:school_id, :graduation,
:admin, :stars, :credits, :school_name
has_many :uploads, :dependent => :destroy
belongs_to :school
has_many :downloads, :source => :user_id, :dependent => :destroy
has_many :comments, :dependent => :destroy
#VALIDATIONS OMITTED
#WARNING
before_create :encrypt_password
#PASSWORD ENCRYPTION METHOD OMITTED
#getter for school name
def school_name
school.name if school
end
#setter for school name (will create school if it didn't find one)
def school_name=(name)
self.school = School.find_by_name(name) unless name.blank?
end
def add_credits(num)
self.credits += num
end
def charge
self.credits -= 1
self.save(false)
end
def has_downloaded?(file)
@downloads = self.downloads.find(:all, :conditions => "upload_id = #{file.id}")
return (@downloads.length > 0)
end
private
#MORE PASSWORD ENCRYPTION LOGIC
end
Here is the code for my upload model:
class Upload < ActiveRecord::Base
default_scope :order => 'uploads.created_at DESC'
attr_protected :linked_file_name, :linked_content_type, :linked_size
attr_accessible :user_id, :stars, :ratings,
:semester, :professor, :year,
:description, :course_id, :school_id
after_save :set_course_school
belongs_to :user
belongs_to :school
belongs_to :course
has_many :downloads, :source => :upload_id, :dependent => :destroy
has_many :comments, :foreign_key => "file_id", :dependent => :destroy
#belongs_to :class
#paperclip
has_attached_file :linked,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":class/:id/:attachment/:basename.:extension"
#validations
validates :school_id, :presence => true
def update_rating
@comments = self.comments.all
if @comments.length > 0
@stars = 0
@comments.each do |comment|
@stars += comment.rating
end
self.stars = @stars
self.ratings = @comments.length
end
self.save(false)
end
def course_name
return [course.subject, course.course_code].join(' ') if course
end
def course_name=(name)
@split = name.split(' ', 2)
@subject = @split.first
@course_code = @split.last
@conditions = {
:subject => @subject,
:course_code => @course_code,
:school_id => self.school_id
}
self.course = Course.find(:first, :conditions => @conditions) || Course.create(@conditions)
end
def set_course_school
course.set_school
end
end
And here is the controller action:
def show
@user = User.find(params[:id])
@uploads = @user.uploads.all
@downloads = @user.downloads.all
end
Heroku seems to be having some problem with the statement @user.uploads.all which works fine locally, here is what the logs give me:
2011-12-29T21:57:07+00:00 app[web.1]: Started GET "/users/1" for 200.88.103.28 at 2011-12-29 13:57:07 -0800
2011-12-29T21:57:07+00:00 app[web.1]: Processing by UsersController#show as HTML
2011-12-29T21:57:07+00:00 app[web.1]: Parameters: {"id"=>"1"}
2011-12-29T21:57:07+00:00 app[web.1]: Completed in 10ms
2011-12-29T21:57:07+00:00 app[web.1]:
2011-12-29T21:57:07+00:00 app[web.1]: ActiveRecord::StatementInvalid (PGError: ERROR: operator does not exist: character varying = integer
2011-12-29T21:57:07+00:00 app[web.1]: : SELECT "uploads".* FROM "uploads" WHERE ("uploads".user_id = 1) ORDER BY uploads.created_at DESC):
2011-12-29T21:57:07+00:00 app[web.1]: ^
2011-12-29T21:57:07+00:00 app[web.1]: app/controllers/users_controller.rb:21:in `show'
2011-12-29T21:57:07+00:00 app[web.1]: HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
2011-12-29T21:57:07+00:00 app[web.1]: LINE 1: ...ROM "uploads" WHERE ("uploads".user_id = 1) ORDER...
Any ideas? I imagine the fix is super simple. What’s weird is that I have another Heroku deployed app that uses the exact same user logic (has a show page that gets all the ‘posts’ of a user) and that works fine. The code looks almost identical…
I would greatly appreciate a solution to this problem. I wish I could offer a bounty but I used most of my rep on a big bounty on an Android question.
From the error statement, it looks like the
user_idcolumn on youruploadstable is a varchar, not an integer. Postgres (used by Heroku) doesn’t automatically cast, as far as I know.Can you confirm the data types?