Monday, December 26, 2011

Create Rails Models From JSON

Recently, I had the need to be able to create and save an active record model based on a json object returned back to the server. After some digging around I came across the following solution that fits the bill.

my_model_params = params[:my_model_params]
my_model = MyModel.new(JSON.parse(my_model_params))

JSON.parse from the ruby json gem allows you to parse a json string into a hash object which can then be used to create your model as you normally would in rails.

No comments:

Post a Comment