【发布时间】:2013-06-25 11:29:59
【问题描述】:
在我的控制器的索引操作中,我返回了一个应该呈现为 JSON 的图片模型数组。
def index
@pictures = Pictures.all
respond_to do |format|
format.json { render json: @pictures.to_json( include: [:imageable] ) }
end
end
此模型配置了多态关联。
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
class Employee < ActiveRecord::Base
has_many :pictures, :as => :imageable
attr_accessible :name
end
class Product < ActiveRecord::Base
has_many :pictures, :as => :imageable
attr_accessible :type
end
图片数组将包含 Picture 对象,该对象具有与 Employee 和 Product 的可成像关联。如何将可成像关联对象以不同方式呈现到 json,包括 Employeee 和 Product 特定字段?
谢谢你, 阿萨夫
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 polymorphic-associations