Guest User

Untitled

a guest
Aug 8th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. 500 error when I deploy my app to Heroku
  2. 2012-01-03T10:33:49+00:00 app[web.1]: Started GET "/" for
  3.  
  4. 2012-01-03T10:33:49+00:00 app[web.1]: Processing by PagesController#home as HTML
  5. 2012-01-03T10:33:49+00:00 app[web.1]:
  6. 2012-01-03T10:33:49+00:00 app[web.1]: Completed in 15ms
  7. 2012-01-03T10:33:49+00:00 app[web.1]:
  8. 2012-01-03T10:33:49+00:00 app[web.1]: app/controllers/pages_controller.rb:7:in `home'
  9. 2012-01-03T10:33:49+00:00 app[web.1]: ActiveRecord::StatementInvalid (PGError: ERROR: syntax error at or near "["
  10. 2012-01-03T10:33:49+00:00 app[web.1]: : SELECT "posts".* FROM "posts" WHERE (user_id IN ([]) OR user_id = 2) ORDER BY posts.created_at DESC LIMIT 30 OFFSET 0):
  11. 2012-01-03T10:33:49+00:00 app[web.1]:
  12. 2012-01-03T10:33:49+00:00 app[web.1]: LINE 1: ...sts".* FROM "posts" WHERE (user_id IN ([]) OR use...
  13. 2012-01-03T10:33:49+00:00 app[web.1]:
  14.  
  15. class PagesController < ApplicationController
  16.  
  17. def home
  18. @title = "Home"
  19. if signed_in?
  20. @post = Post.new
  21. @feed_items = current_user.feed.paginate(:page => params[:page])
  22. end
  23. end
  24.  
  25. class User < ActiveRecord::Base
  26.  
  27. has_many :posts, :dependent => :destroy
  28.  
  29. has_many :relationships, :foreign_key => "follower_id",
  30. :dependent => :destroy
  31. has_many :reverse_relationships, :foreign_key => "followed_id",
  32. :class_name => "Relationship",
  33. :dependent => :destroy
  34.  
  35. has_many :following, :through => :relationships, :source => :followed
  36. has_many :followers, :through => :reverse_relationships, :source => :follower
  37.  
  38. attr_accessor :password
  39. attr_accessible :name, :email, :password, :password_confirmation
  40.  
  41. email_regex = /A[w+-.]+@[a-zd-.]+.[a-z]+z/i
  42.  
  43. validates :name, :presence => true,
  44. :length => { :maximum => 50 }
  45.  
  46. validates :email, :presence => true,
  47. :format => { :with => email_regex },
  48. :uniqueness => { :case_sensitive => false}
  49.  
  50. validates :password, :presence => true,
  51. :confirmation => true,
  52. :length => { :within => 6..40 }
  53.  
  54. before_save :encrypt_password
  55.  
  56. def has_password?(submitted_password)
  57. encrypted_password == encrypt(submitted_password)
  58. end
  59.  
  60. def self.authenticate(email, submitted_password)
  61. user = find_by_email(email)
  62. return nil if user.nil?
  63. return user if user.has_password?(submitted_password)
  64. end
  65.  
  66. def self.authenticate_with_salt(id, cookie_salt)
  67. user = find_by_id(id)
  68. (user && user.salt == cookie_salt) ? user : nil
  69. end
  70.  
  71. def following?(followed)
  72. relationships.find_by_followed_id(followed)
  73. end
  74.  
  75. def follow!(followed)
  76. relationships.create!(:followed_id => followed.id)
  77. end
  78.  
  79. def unfollow!(followed)
  80. relationships.find_by_followed_id(followed).destroy
  81. end
  82.  
  83. def feed
  84. Post.from_users_followed_by(self)
  85. end
  86.  
  87. private
  88.  
  89. def encrypt_password
  90. self.salt = make_salt unless has_password?(password)
  91. self.encrypted_password = encrypt(password)
  92. end
  93.  
  94. def encrypt(string)
  95. secure_hash("#{salt}--#{string}")
  96. end
  97.  
  98. def make_salt
  99. secure_hash("#{Time.now.utc}--#{password}")
  100. end
  101.  
  102. def secure_hash(string)
  103. Digest::SHA2.hexdigest(string)
  104. end
  105.  
  106. end
  107.  
  108. class Post < ActiveRecord::Base
  109. attr_accessible :content
  110.  
  111. belongs_to :user
  112.  
  113. validates :content, :presence => true, :length => { :maximum => 140 }
  114. validates :user_id, :presence => true
  115.  
  116. default_scope :order => 'posts.created_at DESC'
  117.  
  118. scope :from_users_followed_by, lambda { |user| followed_by(user) }
  119.  
  120. def self.from_users_followed_by(user)
  121. following_ids = user.following_ids
  122. where("user_id IN (#{following_ids}) OR user_id = ?", user)
  123. end
  124.  
  125. private
  126.  
  127. def self.followed_by(user)
  128. following_ids = %(SELECT followed_id FROM relationships
  129. WHERE follower_id = :user_id)
  130. where("user_id IN (#{following_ids}) OR user_id = :user_id",
  131. { :user_id => user })
  132. end
  133. end
  134.  
  135. def self.from_users_followed_by(user)
  136. following_ids = user.following_ids
  137. where("user_id IN (#{following_ids}) OR user_id = ?", user)
  138. #------------------^^^^^^^^^^^^^^^^
  139. end
  140.  
  141. where("user_id IN ([]) OR user_id = ?", user)
  142. where("user_id IN ([11, 23, 42]) OR user_id = ?", user)
  143.  
  144. def self.from_users_followed_by(user)
  145. following_ids = user.following_ids
  146. if(following_ids.empty?)
  147. where('user_id = ?', user)
  148. else
  149. where('user_id in (?) or user_id = ?', following_ids, user)
  150. end
  151. end
  152.  
  153. def self.from_users_followed_by(user)
  154. following_ids = user.following_ids
  155. if(following_ids.empty?)
  156. where('user_id = ?', user)
  157. else
  158. following_ids.push(user.id)
  159. where('user_id in (?)', following_ids
  160. end
  161. end
  162.  
  163. def self.from_users_followed_by(user)
  164. where('user_id in (?)', user.following_ids + [user.id])
  165. end
Add Comment
Please, Sign In to add comment