Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing MongoDB with Ruby On Rails and the Mongomapper plugin
    primarykey
    data
    text
    <p>I am currently trying to learn Ruby On Rails as I am a long-time PHP developer so I am building my own community like page.</p> <p>I have came pretty far and have made the user models and such using MySQL.</p> <p>But then I heard of MongoDB and looked in to it a little bit more and I find it kind of nice.</p> <p>So I have set it up and I am using mongomapper for the connection between rails and MongoDB.</p> <p>I'm now using it for the News page on the site.</p> <p>I also have a profile page for every User which includes their own guestbook so other users can come to their profile and write a little message to them.</p> <p>My thought now is to change the User models from using MySQL to start using MongoDB.</p> <p>I can start by showing how the models for each User is set up.</p> <p>The user model:</p> <pre><code>class User &lt; ActiveRecord::Base has_one :guestbook, :class_name =&gt; "User::Guestbook" </code></pre> <p>The Guestbook model model:</p> <pre><code>class User::Guestbook &lt; ActiveRecord::Base belongs_to :user has_many :posts, :class_name =&gt; "User::Guestbook::Posts", :foreign_key =&gt; "user_id" </code></pre> <p>And then the Guestbook posts model:</p> <pre><code>class User::Guestbook::Posts &lt; ActiveRecord::Base belongs_to :guestbook, :class_name =&gt; "User::Guestbook" </code></pre> <p>I have divided it like this for my own convenience but now when i am going to try to migrate to MongoDB i dont know how to make the tables.</p> <p>I would like to have one table for each user and in that table a "column" for all the guestbook entries since MongoDB can have a EmbeddedDocument. I would like to do this so i just have one Table for each user and not like now when i have three tables just to be able to have a guestbook.</p> <p>So my thought is to have it like this:</p> <p>The user model:</p> <pre><code>class User include MongoMapper::Document one :guestbook, :class_name =&gt; "User::Guestbook" </code></pre> <p>The Guestbook model model:</p> <pre><code>class User::Guestbook include MongoMapper::EmbeddedDocument belongs_to :user many :posts, :class_name =&gt; "User::Guestbook::Posts", :foreign_key =&gt; "user_id" </code></pre> <p>And then the Guestbook posts model:</p> <pre><code>class User::Guestbook::Posts include MongoMapper::EmbeddedDocument belongs_to :guestbook, :class_name =&gt; "User::Guestbook" </code></pre> <p>But then i can think of one problem.. That when i just want to fetch the user information like a nickname and a birthdate then it will have to fetch all the users guestbook posts. And if each user has like a thousand posts in the guestbook it will get really much to fetch for the system. Or am I wrong?</p> <p>Do you think I should do it any other way?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload