Home > front end >  how to pass parameters enum in method any?
how to pass parameters enum in method any?

Time:06-01

How to parameterize enum? I want to use my enum value in method any? to check true or false for my ActiveRecord

I have method is_any? to check my ActiveRecord

  def is_any? status
    album.voice_guides.all_except(params[:ids]).any?(&status?)
  end

And call is_any? in this method

  def verify_bulk_destroy
    return if album.pending?

    if album.publish?
      raise Api::Error::ControllerRuntimeError, :error unless is_any? 
       (:publish)
    end
  end

But it raise error

  undefined method `status?' for #<Api::Admin::Albums::PremiumGuidesController:0x0000000000d278>
   Did you mean?  status
                  status=

CodePudding user response:

Just change your is_any? method to

def is_any?(status)
  album.voice_guides.all_except(params[:ids]).any? { |guide| guide.status == status } 
end

which will load all records into memory first as your method did before.

Or to this when you want to check the condition in the database without loading the records into memory which might be faster depending on your needs:

def is_any?(status)
  album.voice_guides.all_except(params[:ids]).where(status: status).exists?
end
  • Related