Commit 9d276308 authored by Steffen van Bergerem's avatar Steffen van Bergerem Committed by Benjamin Neff

Replace where...first with find_by

Fixes #7539

closes #7593
parent a358bf7b
......@@ -45,7 +45,7 @@ class Comment < ApplicationRecord
after_destroy do
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?
end
......
......@@ -38,10 +38,10 @@ class Conversation < ApplicationRecord
end
def set_read(user)
if visibility = self.conversation_visibilities.where(:person_id => user.person.id).first
visibility.unread = 0
visibility.save
end
visibility = conversation_visibilities.find_by(person_id: user.person.id)
return unless visibility
visibility.unread = 0
visibility.save
end
def participant_handles
......@@ -57,7 +57,7 @@ class Conversation < ApplicationRecord
def last_author
return unless @last_author.present? || messages.size > 0
@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
def ordered_participants
......
......@@ -30,7 +30,7 @@ class Like < ApplicationRecord
after_destroy do
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?
end
......
......@@ -15,10 +15,10 @@ class Message < ApplicationRecord
end
def increase_unread(user)
if vis = ConversationVisibility.where(:conversation_id => self.conversation_id, :person_id => user.person.id).first
vis.unread += 1
vis.save
end
vis = ConversationVisibility.find_by(conversation_id: conversation_id, person_id: user.person.id)
return unless vis
vis.unread += 1
vis.save
end
def message
......
......@@ -191,7 +191,7 @@ class Person < ApplicationRecord
def self.find_from_guid_or_username(params)
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])
u.person
else
......
......@@ -124,12 +124,12 @@ class Post < ApplicationRecord
def reshare_for(user)
return unless user
reshares.where(:author_id => user.person.id).first
reshares.find_by(author_id: user.person.id)
end
def like_for(user)
return unless user
likes.where(:author_id => user.person.id).first
likes.find_by(author_id: user.person.id)
end
#############
......
......@@ -24,7 +24,7 @@ class Service < ApplicationRecord
def first_from_omniauth( auth_hash )
@@auth = auth_hash
where( type: service_type, uid: options[:uid] ).first
find_by(type: service_type, uid: options[:uid])
end
def initialize_from_omniauth( auth_hash )
......
......@@ -176,7 +176,7 @@ class User < ApplicationRecord
if pref_hash[key] == 'true'
self.user_preferences.find_or_create_by(email_type: key)
else
block = self.user_preferences.where(:email_type => key).first
block = user_preferences.find_by(email_type: key)
if block
block.destroy
end
......@@ -289,9 +289,9 @@ class User < ApplicationRecord
# @return [Like]
def like_for(target)
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
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
......
......@@ -28,7 +28,7 @@ module User::Querying
def block_for(person)
return nil unless person
self.blocks.where(person_id: person.id).first
blocks.find_by(person_id: person.id)
end
def aspects_with_shareable(base_class_name_or_class, shareable_id)
......@@ -38,7 +38,7 @@ module User::Querying
end
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
# @param [Person] person
......
......@@ -49,7 +49,7 @@ module User::SocialActions
def update_or_create_participation!(target)
return if target.author == person
participation = participations.where(target_id: target).first
participation = participations.find_by(target_id: target)
if participation.present?
participation.update!(count: participation.count.next)
else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment