A
A
Anton Ivanov2016-01-07 01:55:51
Ruby on Rails
Anton Ivanov, 2016-01-07 01:55:51

Why, after redefining the receipt of a field from the model, its change does not work?

Hello.
There is a model. It has an options field, of type json. In the database, this field can be null.
I want that in rails, if in the base this field is null, then I would still return an empty Hash.
I wrote this method (in the model)

def options
  self[:options] || {}
end

But, after that, this construction does not work: direct assignment ( ) works fine. When debugging, I see that when assigning a key (which does not work), execution enters my overridden method. But it's just getting the value. I can't figure out how to make it work :)
instance.options[:some_key] = some_value
instance.options = {some_key: some_value}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeniy Trebin, 2016-01-08
@evgeniy_trebin

can be written like this

def options
  self[:options] ||= {}
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question