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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:48:18+00:00 2026-05-13T09:48:18+00:00

I am trying to figure out how to layout my database for a site

  • 0

I am trying to figure out how to layout my database for a site I am working on. Here are my models:

class User
  include MongoMapper::Document

  // keys here

  many :items
  many :likes
end

class Item
  include MongoMapper::Document

  key :name, String, :required => true

  many :likes
end

class Like
  include MongoMapper::EmbeddedDocument

  key :user_id, String, :required => true
end

I believe the Like should be embedded somewhere, but I am having a hard time picking one because of the functionality I would like to get out of it.

user = User.first
user.likes // should print out all the Items he liked

item = Item.first
item.likes // so I can count how many people like that item

Although the problem comes when using an EmbeddedDocument, you lose the find, and other helpful methods, and you can’t have it embedded in both models. So having it only in Item, I would need to run this (but can’t):

item = Item.first
item.likes.find_by_user_id(User.first._id)

undefined method find_by_user_id will be thrown. So if I was to embed this into my User, I still couldn’t do this.

likes = Like.all // will throw undefined `all`

So I came to the conclusing to maybe do it this way:

class Like
  include MongoMapper::Document

  key :user_id, String, :required => true
  key :item_id, String, :required => true

  belongs_to :user
  belongs_to :item
end

But this seems like I am still trying to do things the old MySQL way. Could anybody give me a point on the most likely way to code this with MongoMapper?

Thanks in advance!

  • 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-13T09:48:19+00:00Added an answer on May 13, 2026 at 9:48 am

    Whether it’s possible to model this in MongoMapper depends on whether there’s data that needs to be stored in the Like model. If, as in your example, there isn’t any data associated with the Like model, there is a way. The most recent update to MongoMapper has added support for many-to-many relationships, although it’s still in the early stages.

    You’d create your models like this:

    class User
      include MongoMapper::Document
      key :name, String, :required => true
      key :liked_item_ids, Array
      many :liked_items, :in => :liked_item_ids, :class_name => "Item"
    end
    
    class Item
      include MongoMapper::Document
      key :name, String, :required => true
      many :fans, :class_name => "User", :foreign_key => :liked_item_ids
    end
    

    Then you can do:

    >> u = User.new( :name => 'emily' )
    >> i = Item.new( :name => 'chocolate' )
    >> u.liked_items << i
    >> u.save
    >> u.liked_items
    => [#<Item name: "chocolate", _id: 4b44cc6c271a466269000001>]
    
    >> i.fans
    => [#<User name: "emily", liked_item_ids: [4b44cc6c271a466269000001], _id: 4b44cc6c271a466269000002>]
    

    Unfortunately, what you can’t do with this setup is add a like from the Item side of the relationship yet. However, there’s an open issue on GitHub about creating a proper reverse for the many :in relationship which will be used, in this instance, as follows:

    many :fans, :class_name => "User", :source => :liked_items
    

    On the other hand, if there is information that needs to be stored in the Like, such as the date the user liked the item, there isn’t a way to model it currently. The ideal setup in this case (disregarding what’s supported in MongoMapper right now) would be similar to what you’ve included in your question. You’d need all three models, with Like embedded in the User model and a has_many :through relationship to create the link from User to Item. Unfortunately, support for this in MongoMapper is probably pretty far away.

    If you’d like to encourage support for behavior like this in MongoMapper, you can ask about it on the mailing list or open an issue on the MongoMapper github repository.

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

Sidebar

Related Questions

I'm trying to figure out how to lay out two of my Django models
Need help trying to figure out why the button onCLick event isn't working. I
I'm trying to figure out how to load/set models/classes in Lithium controller. This is
I've been trying to figure out some layout update xml directives to add, remove
I'm trying to figure out which browsers currently (natively) support the CSS3 layout module
I am trying to figure out how to set the layout from a custom
Im trying to figure out how to align a view in relative layout to
Trying to figure out the best way to set up collection lists for users
Trying to figure out which to use.
Trying to figure out how to programmatically access the web pages in the application.

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.