Forum: Ruby on Rails Get model.create(params) to silently skip unknown attributes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fernando P. (Guest)
on 2009-03-06 18:17
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?
Frederick C. (Guest)
on 2009-03-06 22:41
(Received via mailing list)
On Mar 6, 4:17 pm, Fernando P. <removed_email_address@domain.invalid>
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
Fernando P. (Guest)
on 2009-03-06 23:12
> 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 topic is locked and can not be replied to.