No, by default, serializing a model instance emits only its own attributes, not those of its associations. But you can customize this behavior by including a method in the model as_json:
class MyModel < ActiveRecord::Base
has_many :widgets
def as_json(options={})
{
:name => name,
:widgets => widgets.to_json
}
end
end
You probably also want to define as_jsonin the associated model, otherwise you will get a standard attribute hash.
EDIT
, , . , as_json Widget, MyModel as_json :
def as_json(options={})
{
:name => name,
:widgets => widgets.map(&:as_json)
}
end
, monkeypatch Array # as_json , map.
, . :include .