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

  • Home
  • SEARCH
  • 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 6384635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:48:00+00:00 2026-05-25T02:48:00+00:00

This question relates to using Ruby on Rails 3 with MongoMapper and EmbeddedDocument. Also,

  • 0

This question relates to using Ruby on Rails 3 with MongoMapper and EmbeddedDocument. Also, the ManyAssociation.

http://mongomapper.com/documentation/embedded-document.html

The MongoMapper examples (in the link above) show two separate classes:

class Order
  include MongoMapper::Document

  many :line_items
  timestamps!
end

class LineItem
  include MongoMapper::EmbeddedDocument

  key :name, String
  key :quantity, Integer
end

But this pollutes the global namespace with LineItem. Out of context, LineItem for what? And what if I want another model, say WishList, to also have a LineItem set?

So it is possible to embed the LineItem class inside Order, like this:

class Order
  include MongoMapper::Document

  many :line_items, :class_name => "Order::LineItem"
  timestamps!

  class LineItem
    include MongoMapper::EmbeddedDocument

    key :name, String
    key :quantity, Integer
  end
end

While this may be technically fine (yes?), will I run into design issues later on? Does it just make the code too ugly? Too complex?

Presumably, the existence of this in the Ruby language means someone thinks it’s an ok idea?

One thing I always liked about Django is how it uses “apps” to group related model classes (and separate the namespaces). So my code above is also achieving that in Rails.

  • 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-05-25T02:48:01+00:00Added an answer on May 25, 2026 at 2:48 am

    I don’t see any technical problems to either approach. The problem I’ve run into is that when a class is embedded in it’s parent and is also in the same file, I’ll forget it’s there. So if your namespaces it as Order::LineItem you can make an “order” folder in your “models” folder and put “line_item.rb” in there.

    The other trouble is if you want to have a controller for Order::LineItem, you also have to namespace it and put it in a folder, and in the router it will look like:

    resource :orders do
      resources :line_items,         :controller => "order/line_items"
    end
    

    Unless you know your app is going to have multiple types of line_items, I’d recommend not namespacing it—you could be over-coding if you did. For example, if you were later to need two types of line_items you might even find that some of your code could be re-used between models—and if you namespaced your first line_item you may find yourself de-namespacing it.

    In Python, namespaces are considered a great thing that there should be more of. But when you think about the global namespace of your Rails app, it’s not that cluttered. Gem authors are pretty good about keeping all of their classes namespaced in their one gem module, so in your Rails app you’re going to have one namespace for each gem you’re using (Rails itself is 5 gems, not sure how many global constants though), plus and a bunch of files included by Rails (e.g. SecureRandom). It turns out to be really nice to have those files “just there” and I’ve found in practice that namespace collisions are rare and you can easily work around them. I’ve only run into a namespace issue maybe once, but several times I’ve accidentally defined a “send” method on a model—a much more common problem with similar repercussions.

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

Sidebar

Related Questions

My question relates to this question asked earlier. In situations where I am using
This question relates to ASP.NET MVC3 using the Razor Engine. What I need to
This question relates to this SO question and answer (rails-3-ssl-deprecation ) where its suggested
This question relates to http://www.qtcentre.org/threads/18151-QScrollArea-misbehaving-background-style http://www.qtforum.org/article/34443/cannot-change-background-color-of-a-qscrollarea-with-setstylesheet.html I'm asking in here because I believe the
This relates to this question . I am using the code below from this
This question is related to the this . I'm using the following to extract
This question relates to an ASP.NET website, originally developed in VS 2005 and now
This question relates to this question which I asked earlier this week. The answer
This question relates to those parts of the KenKen Latin Square puzzles which ask
Background: This question relates to versions of Delphi below 2009 (ie without Unicode support

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.