I have the following relationships (using RoR 3.2.13 and ancestry 2.0.0) and REALLY need some help in configuring how the serializer renders with the MenuHeaderSerializer:
class Menu < ActiveRecord::Base
has_many :menu_headers
end
class MenuHeader < ActiveRecord::Base
has_ancestry # the nested relationship
has_many :items
belongs_to :menu
end
class Item < ActiveRecord::Base
belongs_to :menu_header
end
My serializers are pretty explanatory and look like this:
class MenuSerializer < ActiveModel::Serializer
attributes :id, :name, :menu_headers
has_many :menu_headers
end
class MenuHeaderSerializer < ActiveModel::Serializer
attributes :id, :name, :children # <- this needs to be called and wrapped in a MenuHeaderSerializer; it basically just dumps eveything like to_json
#has_many :items
end
So my call to children should return children using MenuHeaderSerializer. Does that make sense?
I have tried what I think are all of the variations like the following:
class MenuHeaderSerializer < ActiveModel::Serializer
attributes :id, :name, :sub # :children #, :sub
def sub
MenuHeaderSerializer.new(children)
#object.children
end
or trying to force children into using the MenuHeaderSerializer but am just at a loss. Any help would be appreciated.
Perhaps something like:
attributes :id, :name, :children serializer: MenuHeaderSerializer # doesn't work
thx
not sure about Ancestery but you can do
has_many :children, each_serializer: MenuHeaderSerializer
Related
I have a Active Model JSON Serializer for a model, which has a has_many relation, that I want to include in the JSON response.
I would like the record to be included as has_one instead of has_many record - to only include the first record:
class PersonSerializer < ActiveModel::Serializer
attributes :name, :symbol
has_many :stats
has_many :body_parts
end
Here, body_parts should be returned as has one :body_part. What is the best way to do that?
If you want to return only the first body_part and do that only in your serializer response, then better to define it as a method in PersonSerializer.
class PersonSerializer < ActiveModel::Serializer
attributes :name, :symbol
has_one :body_part, serializer: BodyPartSerializer
...
def body_part
self.object.body_parts.first
end
end
It assumes that you have a separate serialiser for BodyPart model.
Isn't it as simple as:
class Person
def body_part
body_parts.first
end
# or
has_one :body_part, -> { order(id: :asc) }
end
class PersonSerializer < ActiveModel::Serializer
attributes :name, :symbol
has_many :stats
has_one :body_part
end
In both cases you need to specific what exactly does it mean to be the first body part.
I am using Rails 5.0.1, and am really confused about the following problem. I have few models with polymorphic associations.
class Container < ApplicationRecord
has_many :steps, as: 'parent', dependent: :destroy
end
class Step < ApplicationRecord
belongs_to :parent, polymorphic: true
belongs_to :implementation, polymorphic: true
end
class FirstStep < ApplicationRecord
has_one :step, as: 'implementation'
has_many :params, dependent: :destroy
end
class SecondStep < ApplicationRecord
has_one :step, as: 'implementation'
has_many :headers, dependent: :destroy
end
class Param < ApplicationRecord
belongs_to :first_step
end
class Header < ApplicationRecord
belongs_to :second_step
end
A step associates to an implementation (FirstStep, SecondStep). In addition to it, a container can also be a step's implementation. I'm using Active Model Serializers to serialize the model info to JSON. Following is the related code to serializers.
class StepSerializer < ActiveModel::Serializer
attributes :id, :implementation_type, :implementation_id, :active, :position
belongs_to :implementation
end
class FirstStepSerializer < ActiveModel::Serializer
attributes :name, :params_attributes
def params_attributes
object.params.map { |p| ParamSerializer.new(p).attributes }
end
end
class SecondStepSerializer < ActiveModel::Serializer
attributes :id, :title, :headers_attributes
def headers_attributes
object.headers.map { |p| HeaderSerializer.new(p).attributes }
end
end
class ParamSerializer < ActiveModel::Serializer
attributes :id
end
class HeaderSerializer < ActiveModel::Serializer
attributes :id
end
The implementations of step model can have different attributes, as specified in the model. The problem is, when I write
render json: container.steps
it fires N+1 queries to get the results. How do I optimize it?
Edit 1
Inspired by this answer, I tried to separate objects by their implementation_type, and it worked. What I did was:
# my controller action
def index
steps = []
steps += container.steps.where(implementation_type: 'FirstStep').includes(implementation: [:params])
steps += container.steps.where(implementation_type: 'SecondStep').includes(implementation: [:headers])
render json: steps
end
This prevented the N+1 queries for fetching params and headers, but it doesn't work if a step is a container.
Change your FirstStepSerializer and SecondStepSerializer serializer like following
class FirstStepSerializer < ActiveModel::Serializer
attributes :name
has_many :params, :serializer => ParamSerializer
end
class SecondStepSerializer < ActiveModel::Serializer
attributes :id, :title
has_many :headers, :serializer => HeaderSerializer
end
This might help
Hi the serailizer in my application looks like this
class ProgressSerializer < ActiveModel::Serializer
attributes :id
has_one :race
end
class RaceSerializer < ActiveModel::Serializer
attributes :id
has_many :progresses
end
The has_one and has_many together gives me error stack level too deep.
Things I tried.
config/initializers/active_model_intializer.rb
ActiveModel::Serializer.setup do |config|
config.embed = :ids
config.include = true
end
Second thing I tried
class RaceSerializer < ActiveModel::Serializer
attributes :id
has_many :progresses , :serializer => ProgressSerializer
end
class ProgressSerializer < ActiveModel::Serializer
attributes :id
has_one :race , :serializer => RaceSerializer
end
Models
class Progress < ActiveRecord::Base
belongs_to :race
end
class Race < ActiveRecord::Base
has_many :progresses
end
Can you share your Model.rb files?
If Progress is main then Races underneath, this one should work.
class ProgressSerializer < ActiveModel::Serializer
attributes :id, :races
has_many :races
end
class RaceSerializer < ActiveModel::Serializer
attributes :id
end
then try to delete has_many :progresses and add has_one :race to Race with attributes :id, :race
I would like to use pluck method in Active Model Serializer for an object association:
Post has_many :comments
Is there a way to override
has_many :comments
in the serializer to use pluck(:id, :title) on the comments ?
You can use block with has_many to extend your association with methods. See comment "Use a block to extend your associations" here.
class Post < ActiveRecord::Base
has_many :comments do
def plucked()
select("id, title")
end
end
end
or other approach, from the same link can be, have your custom sql query when fetching comments from the database:
has_many :comments, :class_name => 'Comment', :finder_sql => %q(
SELECT id, title
FROM comments
WHERE post_id = #{id}
)
In rails documentation, It shows, there is an option :select, which can also be used for this purpose.
What I usually do in that situation, I have two different serializers CommentSerializer and CommentInfoSerializer. So CommentInfoSerializer only includes minimum attributes that I want to response in embedded attribute of their parent resource.
class CommentSerializer < ActiveModel::Serializer
attributes :id, :title, :body
end
class CommentInfoSerializer < ActiveModel::Serializer
attributes :id, :title
end
class PostSerializer < ActiveModel::Serializer
has_many :comments, serializer: CommentInfoSerializer
end
This question pertains to AMS 0.8
I've got two models:
class Subject < ActiveRecord::Base
has_many :user_combinations
has_ancestry
end
class UserCombination < ActiveRecord::Base
belongs_to :stage
belongs_to :subject
belongs_to :user
end
And two serializers:
class UserCombinationSerializer < ActiveModel::Serializer
attributes :id
belongs_to :stage
belongs_to :subject
end
class SubjectSerializer < ActiveModel::Serializer
attributes :id, :name, :description, :subjects
def include_subjects?
object.is_root?
end
def subjects
object.subtree
end
end
When a UserCombination is serialized, I want to embed the whole subtree of subjects.
When I try to use this setup I get this error:
undefined method `belongs_to' for UserCombinationSerializer:Class
I tried changing the UserCombinationSerializer to this:
class UserCombinationSerializer < ActiveModel::Serializer
attributes :id, :subject, :stage
end
In this case I get no errors, but the subject is serialized in the wrong way - not using the SubjectSerializer.
My questions:
Shouldn't I be able to use a belongs_to relation in the serializer?
If not - how can I get the wanted behaviour - embedding the subject tree using the SubjectSerializer?
This is not really elegant but it seems to be working :
class UserCombinationSerializer < ActiveModel::Serializer
attributes :id, :stage_id, :subject_id
has_one :subject
end
I don't really like calling has_one whereas it's actually a belongs_to association :/
EDIT: Disregard my comment about has_one/belongs_to ambiguity, the doc is actually pretty clear about it: http://www.rubydoc.info/github/rails-api/active_model_serializers/frames
In Active Model Serializer 0-10-stable, belongs_to is now available.
belongs_to :author, serializer: AuthorPreviewSerializer
belongs_to :author, key: :writer
belongs_to :post
belongs_to :blog
def blog
Blog.new(id: 999, name: 'Custom blog')
end
https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/serializers.md#belongs_to
So you could do:
class UserCombinationSerializer < ActiveModel::Serializer
attributes :id
belongs_to :stage, serializer: StageSerializer
belongs_to :subject, serializer: SubjectSerializer
end
What if you try with something like this:
class UserCombinationSerializer < ActiveModel::Serializer
attributes :subject,
:stage,
:id
def subject
SubjectSerializer.new(object.subject, { root: false } )
end
def stage
StageSerializer.new(object.stage, { root: false } )
end
end