Fernando Perez
2009-Mar-06 16:17 UTC
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Mar-06 20:41 UTC
Re: Get model.create(params) to silently skip unknown attributes
On Mar 6, 4:17 pm, Fernando Perez <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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?You''d be better off cleaning up the params hash, eg with something like cleaned_params = params.slice(*Model.column_names) Fred> -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Fernando Perez
2009-Mar-06 21:12 UTC
Re: Get model.create(params) to silently skip unknown attributes
> cleaned_params = params.slice(*Model.column_names) > > FredDamn! 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---