Question

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?

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top