I am having a problem in my Rails 3.2 application where the virtual attribute sent using JSON is not in the right place in the params hash. Well, this is not the place where I expect. It remains to be seen whether my expectations are true. :)
I have a model using a standard virtual attribute template, for example:
class Track < ActiveRecord::Base def rating
The JSON sent to my controller is as follows:
{"id":1,"name":"Icarus - Main Theme 2","rating":2}
To be clear, the name and identifier are not virtual, the rating is.
I end up with this in the hash parameters after the rails do their magic:
{"id"=>"1", "name"=>"Icarus - Main Theme 2", "rating"=>2, "track"=>{"id"=>"1", "name"=>"Icarus - Main Theme 2"}}
As you can see, id and name go to the hash file of the attached track: but there is no rating. Is this expected behavior? It violates the (somewhat) standard practice of using a nested hash in the controller, because the nested hash does not contain all the parameters I need.
Track.update(params[:id], params[:track]) # :track is missing rating
Thanks for your help!
source share