Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: strong_params and getter/setter methods

I'm using strong_params in Rails 4. When I try using a custom setter method like this:

def foo=(foo)
  @foo = foo
end

Then Rails records the attribute as null within the database. If I remove the custom setter, then everything works fine. In Rails 3, custom setters were written like this, but perhaps things have changed? If so, how do custom setters now work?

like image 847
nullnullnull Avatar asked Jan 26 '26 13:01

nullnullnull


1 Answers

I don't think this code worked on Rails 3. Are you certain about this? Because if you do this, you override the setter method created by ActiveRecord (which is responsible for storing data to DB). Does your custom version of method look exactly like shown? What's the point of it?

Try calling super.

def foo=(foo)
  @foo = foo
  super
end

P.S.: I checked and it does not work in Rails 3 (works with super).

like image 145
Sergio Tulentsev Avatar answered Jan 28 '26 06:01

Sergio Tulentsev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!