In Rails 3, is there a way to produce an ActiveRecord object from xml in a controller without writing code yourself to explicitly parse it? Say for example, could a controller receive xml like
<user>
<first_name>Bob</first_name>
<last_name>Smith</last_name>
</user>
and have it produce a proper User object similar to User.new(params[:user])? This is for an api.
Yes, you can do it like this:
Update
On overriding you can do something like this:
Please note that the most important line in the overriding is the
super(xml_data)which will take care on calling the originalfrom_xml(xml_data)of theActiveRecordmodel.So you can customize the rest as needed, but this line is neede if you want to get the original functionality as well.
Let me know if something is not clear.