I have a model
class Banner < ActiveRecord::Base
validates :title, presence: true, length: { maximum: 50 }
validates :description, length: { maximum: 200 }
belongs_to :document
def img_url
document.medium_url
end
end
and serializer
class BannerSerializer < ActiveModel::Serializer
attributes :id, :title, :description, :img_url, :document_id
end
When I'm using render json: Banner.all, it response correctly (has "img_url" in the responding
{
"banners": [
{
"id": 1,
"title": "This is title of banner",
"description": "This is long description...",
"img_url": "http://localhost:3000//system/documents/attachments/000/000/023/medium/one-piece.jpg?1459601536",
"document_id": 23
}
]
}
But when I want to return with other object by using. example:
render json: {
banners: Banner.all,
blogs: Blog.all,
partners: Partner.all
}
The responding don't exist "img_url" (it don't use Serializer).
Please help.
Blog.all.to_json?as_json?ActiveModel::SerializableResource.new(Banner.all).serializable_hashrender json: BlogSerializer.new(Blog.all)