Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8766725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:37:27+00:00 2026-06-13T16:37:27+00:00

This question pertains to AMS 0.8 I’ve got two models: class Subject < ActiveRecord::Base

  • 0

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:

  1. Shouldn’t I be able to use a belongs_to relation in the serializer?
  2. If not – how can I get the wanted behaviour – embedding the subject tree using the SubjectSerializer?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:37:29+00:00Added an answer on June 13, 2026 at 4:37 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question pertains to (at least) CPython 2.7.2 and 3.2.2. Suppose we define Class
thank you for your time, (As a side note, this question pertains mostly to
This question pertains to MVC (Model-View-Controller). My Model currently updates my View when it
To all: This question pertains to some MS research I am doing. What I
I have two tables pertaining to this question: conversations has many messages . The
This question pertains primarily to good design. Suppose I have a controller action like
NOTE: This question is tricky (to decide whether it pertains to SO or not),
This question pertains specifically to shell scripts, but could be about any programming language.
This question pertains to xperf and xperfview, utilities that are part of the Windows
This question pertains to query optimization using Linq with the Entity Framework. Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.