ruby-on-railsrubybefore-save

Check a value before saving


I'm trying to implement a condition in my form which say if cat:(integer)=1 @post.update(father: "lolo")

I'd try put this into my create action, and too into a before_save but is not working. So if you have any ideas to solve this, you're welcome !!

My code :

Option 1 (into the create action)=

if @self.cat = 1
 @cycle.update(father: "lolo")
end 

Option 2 (before_save action )

before_save :check_cat

def check_cat
  @cow = self.cow
  if @cow.cycles.any?
    self.update(father: "lolo")
  end 
end

Solution

  • if @self.cat = 1
    

    You are using assignment in if operator. Must use comparison. Read this for details: https://www.tutorialspoint.com/ruby/ruby_operators.htm

    in short must be

    if @self.cat == 1