Search code examples
ruby-on-railsvalidationparametersmodelrails-activestorage

Ruby On Rails - How to pass params through a custom before_validation function


I have a function that needs params passed. The function is:

private

def file_type(file)
 # code content
end

I am trying to access the code from the same model file using these methods:

has_one_attached :image, dependent: :destroy
has_one_attached :image1, dependent: :destroy

before_validation :file_type, file: :image
before_validation :file_type, file: :image1

before_validation :file_type(params[:image])
before_validation :file_type(params[:image1])

And a few others but non of them work. Is there a way to pass in the params without having to create a new validation class file


Solution

  • Create a method in your class that passes the parameters.

    before_validation :file_types
    
    def file_types
      file_type(image)
      file_type(image1)
    end