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 8023123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:28:42+00:00 2026-06-04T22:28:42+00:00

Assume we have something like: class Company include Mongoid::Document has_many :users field :name, type:

  • 0

Assume we have something like:

class Company
  include Mongoid::Document
  has_many :users
  field :name, type: String
end

class User
  include Mongoid::Document
  belongs_to :company
  field :name, type: String
end

module CompanyRepresenter
  include Roar::Representer::JSON 
  property :name
end

module UserRepresenter
  include Roar::Representer::JSON 
  property :name
  link :self do
    user_url
  end
end

Then we go on to do something like this:

user.extend(UserRepresenter).to_json

And everything is just great. But how about:

User.all.to_json

or:

company.extend(CompanyRepresenter).users.to_json?

or even:

company.users.collect{|u| u.extend(UserRepresenter)}.extend(Representable::JSON::Collection).to_json

The result is always an array of Mongoid’s normal to_json entries.

The question is how do I get something like Company.all.to_json to have Roared JSONs including the links and other extra serialization data.

  • 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-04T22:28:43+00:00Added an answer on June 4, 2026 at 10:28 pm

    It turns out this is simple to achieve using Representable (which is used by Roar):

    require 'roar/representer/json'
    
    module PostRepresenter
      include Roar::Representer::JSON
    
      property :id
      property :title
    end
    
    require 'representable/json/collection'
    
    module PostsRepresenter
      include Representable::JSON::Collection
    
      items extend: PostRepresenter
    end
    
    Post.limit(10).extend(PostsRepresenter)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

assume in Scala I have something like: class NSV(val name:String,val value:String) { } and
Assume I have a business object like this, class Employee { public string name;
Assume I have a C# class like this: [XmlRoot(floors)] public class FloorCollection { [XmlElement(floor)]
I'm writing a Ruby class that extends TCPSocket . Assume it looks something like
Have a class definition that looks something like this class someClass(models.Model): field1 = models.ForeignKey('other_app.field4')
Let's say I have a class like this (and also further assume that all
Assume I have such simple model: class Foo(models.Model): name = models.CharField(max_length=25) b_date = models.DateField()
Assume we have a button declared in XML : <button x:Class=A Content=click me Name=button
Let's assume that I have a method in class like this: - (void)doSomethingOnlyWhenNeeded {
Assume you have several arbitrary classes like below: class Foo { $foovar; public function

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.