Replace where...first with find_by

Fixes #7539

closes #7593
This commit is contained in:
Steffen van Bergerem 2017-08-27 21:42:48 +02:00 committed by Benjamin Neff
parent a358bf7b66
commit 9d2763089d
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
10 changed files with 21 additions and 21 deletions

View file

@ -45,7 +45,7 @@ class Comment < ApplicationRecord
after_destroy do after_destroy do
self.parent.update_comments_counter self.parent.update_comments_counter
participation = author.participations.where(target_id: post.id).first participation = author.participations.find_by(target_id: post.id)
participation.unparticipate! if participation.present? participation.unparticipate! if participation.present?
end end

View file

@ -38,10 +38,10 @@ class Conversation < ApplicationRecord
end end
def set_read(user) def set_read(user)
if visibility = self.conversation_visibilities.where(:person_id => user.person.id).first visibility = conversation_visibilities.find_by(person_id: user.person.id)
visibility.unread = 0 return unless visibility
visibility.save visibility.unread = 0
end visibility.save
end end
def participant_handles def participant_handles
@ -57,7 +57,7 @@ class Conversation < ApplicationRecord
def last_author def last_author
return unless @last_author.present? || messages.size > 0 return unless @last_author.present? || messages.size > 0
@last_author_id ||= messages.pluck(:author_id).last @last_author_id ||= messages.pluck(:author_id).last
@last_author ||= Person.includes(:profile).where(id: @last_author_id).first @last_author ||= Person.includes(:profile).find_by(id: @last_author_id)
end end
def ordered_participants def ordered_participants

View file

@ -30,7 +30,7 @@ class Like < ApplicationRecord
after_destroy do after_destroy do
self.parent.update_likes_counter self.parent.update_likes_counter
participation = author.participations.where(target_id: target.id).first participation = author.participations.find_by(target_id: target.id)
participation.unparticipate! if participation.present? participation.unparticipate! if participation.present?
end end

View file

@ -15,10 +15,10 @@ class Message < ApplicationRecord
end end
def increase_unread(user) def increase_unread(user)
if vis = ConversationVisibility.where(:conversation_id => self.conversation_id, :person_id => user.person.id).first vis = ConversationVisibility.find_by(conversation_id: conversation_id, person_id: user.person.id)
vis.unread += 1 return unless vis
vis.save vis.unread += 1
end vis.save
end end
def message def message

View file

@ -191,7 +191,7 @@ class Person < ApplicationRecord
def self.find_from_guid_or_username(params) def self.find_from_guid_or_username(params)
p = if params[:id].present? p = if params[:id].present?
Person.where(:guid => params[:id]).first Person.find_by(guid: params[:id])
elsif params[:username].present? && u = User.find_by_username(params[:username]) elsif params[:username].present? && u = User.find_by_username(params[:username])
u.person u.person
else else

View file

@ -124,12 +124,12 @@ class Post < ApplicationRecord
def reshare_for(user) def reshare_for(user)
return unless user return unless user
reshares.where(:author_id => user.person.id).first reshares.find_by(author_id: user.person.id)
end end
def like_for(user) def like_for(user)
return unless user return unless user
likes.where(:author_id => user.person.id).first likes.find_by(author_id: user.person.id)
end end
############# #############

View file

@ -24,7 +24,7 @@ class Service < ApplicationRecord
def first_from_omniauth( auth_hash ) def first_from_omniauth( auth_hash )
@@auth = auth_hash @@auth = auth_hash
where( type: service_type, uid: options[:uid] ).first find_by(type: service_type, uid: options[:uid])
end end
def initialize_from_omniauth( auth_hash ) def initialize_from_omniauth( auth_hash )

View file

@ -176,7 +176,7 @@ class User < ApplicationRecord
if pref_hash[key] == 'true' if pref_hash[key] == 'true'
self.user_preferences.find_or_create_by(email_type: key) self.user_preferences.find_or_create_by(email_type: key)
else else
block = self.user_preferences.where(:email_type => key).first block = user_preferences.find_by(email_type: key)
if block if block
block.destroy block.destroy
end end
@ -289,9 +289,9 @@ class User < ApplicationRecord
# @return [Like] # @return [Like]
def like_for(target) def like_for(target)
if target.likes.loaded? if target.likes.loaded?
return target.likes.detect{ |like| like.author_id == self.person.id } target.likes.find {|like| like.author_id == person.id }
else else
return Like.where(:author_id => self.person.id, :target_type => target.class.base_class.to_s, :target_id => target.id).first Like.find_by(author_id: person.id, target_type: target.class.base_class.to_s, target_id: target.id)
end end
end end

View file

@ -28,7 +28,7 @@ module User::Querying
def block_for(person) def block_for(person)
return nil unless person return nil unless person
self.blocks.where(person_id: person.id).first blocks.find_by(person_id: person.id)
end end
def aspects_with_shareable(base_class_name_or_class, shareable_id) def aspects_with_shareable(base_class_name_or_class, shareable_id)
@ -38,7 +38,7 @@ module User::Querying
end end
def contact_for_person_id(person_id) def contact_for_person_id(person_id)
Contact.where(:user_id => self.id, :person_id => person_id).includes(:person => :profile).first Contact.includes(person: :profile).find_by(user_id: id, person_id: person_id)
end end
# @param [Person] person # @param [Person] person

View file

@ -49,7 +49,7 @@ module User::SocialActions
def update_or_create_participation!(target) def update_or_create_participation!(target)
return if target.author == person return if target.author == person
participation = participations.where(target_id: target).first participation = participations.find_by(target_id: target)
if participation.present? if participation.present?
participation.update!(count: participation.count.next) participation.update!(count: participation.count.next)
else else