weixin_39869378
weixin_39869378
2021-01-12 16:31

ams 0.9.3 not embedding ids or sideloading objects (stack level too deep)

Hey everybody,

I'm using rails 4.2.1, rails-api, and ams 0.9.3. My initializer, config/initializers/active_model_serializers.rb. has the following:

 ruby
ActiveModel::Serializer.setup do |config|
  config.embed = :ids
  config.embed_in_root = true
end

Here are my serializers:

 ruby
# breed_serializer.b
class BreedSerializer < ActiveModel::Serializer
  attributes :id, :name, :dogs_count
  has_many :dogs
end

# dog_serializer.rb
class DogSerializer < ActiveModel::Serializer
  attributes :id, :name
  has_one :breed
end

Unfortunately, here's what happens when I run BreedSerializer.new(Breed.first).to_json in the console:


Breed Load (1.1ms)  SELECT  "breeds".* FROM "breeds"  ORDER BY "breeds"."id" ASC LIMIT 1
Dog Load (2.1ms)  SELECT "dogs".* FROM "dogs" WHERE "dogs"."breed_id" = $1  [["breed_id", 1]]
SystemStackError: stack level too deep

该提问来源于开源项目:rails-api/active_model_serializers

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

9条回答

  • weixin_39525097 weixin_39525097 4月前

    Unfortunately, the lack of sideloading is crippling. EmberData currently has a bug WRT async belongsTo relationships (you can't set/save them) and sideloading is the only supported/scalable way to do non-async relationships. So basically this blows the whole stack out of the water.

    点赞 评论 复制链接分享
  • weixin_39861882 weixin_39861882 4月前

    I'll be releasing a PR for sideloading sometime this week.. Whenever I find the time to do it. (I need sideloading, too) :-)

    点赞 评论 复制链接分享
  • weixin_39869378 weixin_39869378 4月前

    Possible duplicate of this issue: https://github.com/rails-api/active_model_serializers/issues/784

    点赞 评论 复制链接分享
  • weixin_39869378 weixin_39869378 4月前

    I fixed this issue by regressing to ams 0.8.3 and using embed :ids, include: true in each serializer.

    点赞 评论 复制链接分享
  • weixin_39881760 weixin_39881760 4月前

    Hi, I had same problem having ams 0.9.3 and same solution worked. I was getting an infinite loop that crashed the Linux process. the serializer: class UserSerializer < ActiveModel::Serializer embed :ids attributes :id, :email, :created_at, :updated_at, :auth_token has_many :products end

    点赞 评论 复制链接分享
  • weixin_39789979 weixin_39789979 4月前

    same problem here

    Models:

    
    class Opportunity < ActiveRecord::Base
      has_one :statusable, as: :target, :dependent => :destroy
      has_one :status, :through => :statusable
    
      has_one :sourceable, as: :target, :dependent => :destroy
      has_one :source, :through => :sourceable
    
      has_one :employeeable, as: :target, :dependent => :destroy
      has_one :employee, :through => :employeeable
    
      belongs_to :customer
      belongs_to :industry
      belongs_to :product
      belongs_to :company
      belongs_to :leader, class_name: 'Employee'
      belongs_to :client_champion, class_name: 'Employee'
    end
    
    class Employeeable < ActiveRecord::Base
      belongs_to :target, polymorphic: true
      belongs_to :employee
    end
    
    class Employee < ActiveRecord::Base
      belongs_to :company
      has_many :customers, foreign_key: 'client_champion_id'
    
      has_many :employeeables, :dependent => :destroy
      has_many :opportunities, :through => :employeeables, :source => :target, :source_type => 'Opportunity'
    end
    

    Serializer:

    
    class OpportunitySerializer < ActiveModel::Serializer
      delegate :attributes, to: :object
    
      has_one :employee, embed: :ids
      has_one :status,  embed: :id
      has_one :source,  embed: :id
    end
    
    class EmployeeSerializer < ActiveModel::Serializer
      delegate :attributes, to: :object
    
      has_many :opportunities, embed: :ids
      has_many :customers, embed: :ids
    end
    
    
    点赞 评论 复制链接分享
  • weixin_39789979 weixin_39789979 4月前

    temporary i did:

    
    class OpportunitySerializer < ActiveModel::Serializer
      delegate :attributes, to: :object
    
      has_one :employee, embed: :ids, serializer: IdSerializer
      has_one :status,  embed: :id
      has_one :source,  embed: :id
    end
    
    class EmployeeSerializer < ActiveModel::Serializer
      delegate :attributes, to: :object
    
      has_many :opportunities, embed: :ids
      has_many :customers, embed: :ids
    end
    
    class IdSerializer < ActiveModel::Serializer
      attributes :id
    end
    
    点赞 评论 复制链接分享
  • weixin_39777464 weixin_39777464 4月前

    ppl I'm sorry about this, we are deeply focus on 0.10, and short-handed to handle the bugs and features request on older version for now. But it's on our radar. :smile:

    点赞 评论 复制链接分享
  • weixin_39902598 weixin_39902598 4月前

    :+1: let's focus on 0.10 and sort out the other issues later.

    点赞 评论 复制链接分享

相关推荐