我有兩種模式:Cabinet和Workplace。Rails to_json belongs_to對象
class Cabinet < ActiveRecord::Base
def as_json(options={})
options.merge!({except: [:created_at, :updated_at]})
super(options)
end
end
class Workplace < ActiveRecord::Base
belongs_to :cabinet
def as_json(options = {})
options.merge!(:except => [:created_at, :updated_at, :cabinet_id], include: :cabinet)
super(options)
end
end
當我打電話Cabinet.first.to_json我得到
{
id: 1,
cabinet: "100"
}
,但是當我打電話Workplace.first.to_json ID獲得
{
name: "first workplace",
Cabinet: {
id: 1,
cabinet: "100",
created_at: "#created_at",
updated_at: "#updated_at"
}
}
爲什麼呢?謝謝並對不起我的英語:)
對不起,我錯了把問題。我會得到內閣對象而不created_at和的updated_at字段:'''{ 名: 「第一工作場所」, 內閣:{ ID:1, 櫃: 「100」 } }''' – motoroller