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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:42:11+00:00 2026-06-13T13:42:11+00:00

I am using mongoid 3 in a rails 3 application. I have a client

  • 0

I am using mongoid 3 in a rails 3 application.

I have a client class with referenced object ‘files’ (so instances of a custom ‘LocalisedFile’ class.)

Client.rb:

class Client
    include Mongoid::Document
    include Mongoid::Timestamps
    store_in collection: 'clients'

    field :name, type: String

    has_many :files, class_name: 'LocalisedFile', inverse_of: :owner
end

LocalisedFile.rb:

class LocalisedFile
    include Mongoid::Document
    include Mongoid::Timestamps
    include Geocoder::Model::Mongoid
    store_in collection: 'files'

    belongs_to :owner, class_name: 'Client', inverse_of: :files
end

No problem to manage my documents.

But when I want to render an array of files, I just get a “owner_id” field with the client string id…

[(2)
    {
        "_id": "508e85e412e86a2607000005",
        "created_at": "2012-10-29T13:34:29Z",
        "owner_id": "508c06e4bcd7ac4108000009",
        "title": "Try",
        "updated_at": "2012-10-29T13:34:29Z",
    },-
    {
        "_id": "508e8c5312e86a2607000006",
        "created_at": "2012-10-29T14:01:56Z",
        "owner_id": "508c06e4bcd7ac4108000009",
        "title": "2nd Try",
        "updated_at": "2012-10-29T14:01:56Z",
    }-
]

That’s maybe normal, but I would like to get the clients informations, to use it in a JS application with Google Maps API, like this :

[(2)
    {
        "_id": "508e85e412e86a2607000005",
        "created_at": "2012-10-29T13:34:29Z",
        "owner": {
            "_id": "508c06e4bcd7ac4108000009",
            "name": "Client 1"
        },
        "title": "Try",
        "updated_at": "2012-10-29T13:34:29Z",
    },-
    {
        "_id": "508e8c5312e86a2607000006",
        "created_at": "2012-10-29T14:01:56Z",
        "owner": {
            "_id": "508c06e4bcd7ac4108000009",
            "name": "Client 1"
        },
        "title": "2nd Try",
        "updated_at": "2012-10-29T14:01:56Z",
    }-
]

Anyone have an idea ?
I would like to test something like the to_hash method but it does not work…

  • 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-13T13:42:12+00:00Added an answer on June 13, 2026 at 1:42 pm

    Since you’re using a referenced relation between Client and LocalisedFile, the client’s data does not get replicated inside the file objects, only the owner_id, to make the relation work. You need to access the client data through the owner relation you defined on the LocalisedFile model. For example:

    l = LocalisedFile.first
    l.owner.id # returns the id of the owner
    l.owner.name # returns the name of the owner
    

    To create the kind of output you need, I’d suggest abstracting this into an instance method with something like:

    class LocalisedFile
      def as_hash_with_owner
        hash = self.to_hash
        hash[:owner] = { _id: self.owner.id, name: self.owner.name }
        hash.except[:owner_id]
      end
    end
    

    Then you can do something like:

    files = LocalisedFile.all.entries # or whatever criteria
    files.map { |f| f.as_hash_with_owner }
    

    This should give you a ruby array of hashes which you can then convert to JSON or whatever format you need.

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

Sidebar

Related Questions

I have a class User in Rails using Mongoid and Devise. I can't seem
Using Mongoid 2.4.5 on Rails 3.2.1 I have a Model Book that has_many :pages
I have recently been getting my feet wet in MongoDB using Mongoid w/ Rails
I have been using mysql in a new rails application, but now I wanted
I have two entities, projects and users. These are modeled in Rails using Mongoid
I'm using Mongoid, Devise and Rails 3.1. I have four models: Students, Teacher, Parents
I have a Rails 3.1 app, running on Ruby 1.9.2, Mongo 2.0.2, using Mongoid
I'm using rails 3, and began my application with ActiveRecord. Now, I have many
I'm writing a ruby on rails application using mongoid and I'm wondering about the
I am trying to initialise mongoid from a rails 3.2 application using ruby-1.9.2 with

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.