mardi 7 mai 2019

How to find ActiveRecord object that has a unique column values compared to other closeby objects

I have a boolean column called active in a table called user.

I'm trying to find users where the user before and after the current_user do not have the same value in column active.

def has_unique_active_value
  user_ids_to_check = [self.id + 1, self.id - 1] # user.id before and after current_user
  if User.where(id: user_ids_to_check, active: self.active).present?
    return nil
  else
    return self
  end
end

current_user = User.all.first.has_unique_active_value

The above should return a user if the active column value is unique to the user before and after. if not, it should return nil

Would there be an easier or better practice to use in order to get the outcome I'm after?

Aucun commentaire:

Enregistrer un commentaire