diff --git a/app/models/comment.rb b/app/models/comment.rb index 4b6baa250..45603e420 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/app/models/conversation.rb b/app/models/conversation.rb index f597af001..43f37f4c1 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -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 diff --git a/app/models/like.rb b/app/models/like.rb index 828d5b8aa..09776aa8b 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -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 diff --git a/app/models/message.rb b/app/models/message.rb index 95992e689..07e952fff 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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 diff --git a/app/models/person.rb b/app/models/person.rb index 87a0f02cf..ca7210d52 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -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 diff --git a/app/models/post.rb b/app/models/post.rb index e592400f6..53157702f 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 ############# diff --git a/app/models/service.rb b/app/models/service.rb index 890fc02bc..835e92e4c 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -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 ) diff --git a/app/models/user.rb b/app/models/user.rb index d596dd9d4..f57c1626a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/models/user/querying.rb b/app/models/user/querying.rb index f9ff0f001..11a3e186c 100644 --- a/app/models/user/querying.rb +++ b/app/models/user/querying.rb @@ -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 diff --git a/app/models/user/social_actions.rb b/app/models/user/social_actions.rb index a01789a3c..d110a81c7 100644 --- a/app/models/user/social_actions.rb +++ b/app/models/user/social_actions.rb @@ -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