0

我有这些课程:

class Product
 attr_accessible :name, ...
 has_many :images
end

class Image
 attr_accessible :image, :position, :product_id, :title
 belongs_to :product
end

行动:

def list
  render :json => {:Result => "OK", :Records => Product.all}
end

如何将每个产品的自己的图像包含为嵌套属性,而不是枚举产品中的所有属性?

4

1 回答 1

0

您可以使用to_json(:include => :attr)

def list
  render :json => {:Result => "OK", :Records => Product.all}.to_json(:include => :images)
end
于 2013-03-27T00:11:48.560 回答