0

I have a controller with a method

def create
   if passenger.valid?
   ....
end

In my Passenger model I have my validation rules. Those rules depend on from which method the validator has been called.

So the question is, how can I get inside my model the name method that called the validator, in this case 'create'?

almo
  • 6,107
  • 6
  • 43
  • 86
  • Possible duplicate of [How to get the name of the calling method?](https://stackoverflow.com/questions/5100299/how-to-get-the-name-of-the-calling-method) – jvillian Jan 19 '18 at 16:13
  • This does not solve my question since it only returns the path of the model. – almo Jan 19 '18 at 16:17
  • Perhaps it would be better to rephrase the question as either getting the caller name OR ways to implement conditional validation, rather than combining the two. – Nathan Jan 19 '18 at 16:23

2 Answers2

0

Try this:

caller_locations(1,1)[0].label
Ronan Lopes
  • 3,320
  • 4
  • 25
  • 51
0

I would suggest setting an attr_accessor in the model for this case

# In model
attr_accessor :validation_caller

def valodation_rules
  if @validation_caller.eql?('create')
    # validations
  else
    # validations
  end
end

# in controller
model_instance.validation_caller = 'create'

Hope it helps..

Md. Farhan Memon
  • 6,055
  • 2
  • 11
  • 36