Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update attributes before_save callback not saving extra attributes

I'm using a before_save callback method to set an attribute to true if a number of other attributes are not 0. The callback is called when I update my model but the extra attribute isn't set. I think it might be because that attribute isn't passed to the update method. How do I work round that?

Controller

@blog.update(blog_params)

Model

before_save do
  self.indicator = true unless attribute_1 == "0" && attribute_2 == "0"
end
like image 698
Mark Robinson Avatar asked Oct 29 '25 22:10

Mark Robinson


1 Answers

You might consider using before_validation instead. Rails will call thebefore_validation callback before it calls before_save, so if you need to make sure you're setting a value, that would be the place to do so.

like image 175
NM Pennypacker Avatar answered Oct 31 '25 14:10

NM Pennypacker