'How can i change a field in a model as per another field?

Greetings fellow developers. I have a model candidate.rb and there are 2 columns(offer_code and candidate_type) that i want to work with. I want the offer_code = "CHP" if the candidate_type = "chapter". I am trying to do this with after_save but this is creating all the new candidates with candidate_type = "chapter". I think the condition that I have used is wrong. Please guide me

In candidate.rb

After_save : chapter_offer

def chapter_offer
 If self.candidate_type =  "chapter"
    self.offer_code = "CHP"  
 end
end


Solution 1:[1]

You are using = for comparison instead of ==

And it will be better if you did something like this

before_save :chapter_offer, if: -> { candidate_type == 'chapter' }

def chapter_offer
  offer_code = 'CHP'
end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AbdUlRahman Shawareb