Get model.create(params) to silently skip unknown attributes

Hi,

I have a remote website that will be sending me some data which I want
to save in one of my models. The problem is that it will also send some
attributes such as attribute_1, attribute_2, etc which I don’t want to
save.

Currently if I do:

my_model.create(params), it will not pass as it has hash keys that don’t
map to any attribute in the model, how to make my model skip these
unknown attributes in a clean manner?

On Mar 6, 4:17 pm, Fernando P. [email protected]
wrote:

map to any attribute in the model, how to make my model skip these
unknown attributes in a clean manner?

You’d be better off cleaning up the params hash, eg with something
like

cleaned_params = params.slice(*Model.column_names)

Fred

cleaned_params = params.slice(*Model.column_names)

Fred

Damn! I was looking for a way to do that, and by default Ruby doesn’t
offer the slice method for hashes, thanks to you I discovered that it is
bundled in ActiveSupport!

Thank you very much for your tip.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs