Some additional testing in the action of the controller:
format.json { render :json => @country }
And in the model:
class Country < ActiveRecord::Base has_many :languages def as_json(options={}) super( :include => [:languages], :except => [:created_at, :updated_at] ) end end
Outputs:
{ created_at: "2010-05-27T17:54:00Z" id: 123 name: "Uzbekistan" updated_at: "2010-05-27T17:54:00Z" }
However, explicitly adding .to_json () to the render statement in the class, and the primary to_json in the model (instead of as_json) gives the expected result. Wherein:
format.json { render :json => @country.to_json() }
in my controller action, and below the override in the model works:
class Country < ActiveRecord::Base has_many :languages def to_json(options={}) super( :include => [:languages], :except => [:created_at, :updated_at] ) end end
Outlets ...
{ id: 123, name: "Uzbekistan", languages: [ {id: 1, name: "Swedish"}, {id: 2, name: "Swahili"} ] }
... which is the expected exit. Did I find a mistake? Did I win a prize?
source share