diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 0d8f6784..32d0229f 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -1,6 +1,6 @@ class UserController < ApplicationController def show - @user = User.find_by_screen_name!(params[:username]) + @user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first @answers = @user.answers.reverse_order.paginate(page: params[:page]) respond_to do |format| format.html @@ -23,7 +23,7 @@ class UserController < ApplicationController def followers @title = 'Followers' - @user = User.find_by_screen_name!(params[:username]) + @user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first @users = @user.followers.reverse_order.paginate(page: params[:page]) @type = :friend render 'show_follow' @@ -31,7 +31,7 @@ class UserController < ApplicationController def friends @title = 'Following' - @user = User.find_by_screen_name!(params[:username]) + @user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first @users = @user.friends.reverse_order.paginate(page: params[:page]) @type = :friend render 'show_follow' diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 5084084a..5c664c4a 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -13,11 +13,7 @@ class Inbox < ActiveRecord::Base end def remove - unless self.question.user.nil? - self.question.user.decrement! :asked_count if self.question.answer_count == 1 - end - - self.question.destroy if self.question.answer_count == 1 + self.question.destroy if self.question.can_be_removed? self.destroy end end diff --git a/app/models/question.rb b/app/models/question.rb index adc1f802..0408a02b 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -3,4 +3,11 @@ class Question < ActiveRecord::Base has_many :answers validates :content, length: { maximum: 255 } + + def can_be_removed? + return false if self.answers.count > 0 + return false if Inbox.where(question: self).count > 1 + self.user.decrement! :asked_count + true + end end