Mondoid Integration

I am developing a Ruby on Rails 3 application, and this is the first time I am using MongoDB.

I thought about this problem in a couple of days, and I did not find a good solution. I want to discuss two issues.

  • Model
  • How to integrate this with Devise

The application is a school course manager. He runs many schools in which there are many students, courses, professors and tasks.

The structure of one school - there are many:

  • School

    • director
    • Students
    • Professors
    • Courses
    • Tasks
  • The principal is the administrator of the school. He is the only one who can create professors, students and courses.

  • There is exactly 1 director.
  • Courses can be (0 - a lot).
  • Maybe (0 - many) students
  • There may be (0 - many) professors
  • Students can attend courses (0 - many).
  • Professors can be in the know (0 - a lot).
  • For each course, there may be (0-many) tasks associated with (0 - many) students. Yes, a professor can send specific assignments to specific students.

I’m at least a couple of days in the architecture of this collection of models on MongoDB using Mongoid, and I am reaching a possible solution. However, I came from the world of relational databases and maybe this is a terrible decision and I am abusing "embed" :)

  • Collection schools
    • School 1
      • Insert Director
      • embed Users
      • embed Courses
      • embed Tasks
    • School 2
      • Insert Director
      • embed Users
      • embed Courses
      • embed task

Models:

class User include Mongoid::Document field :first_name field :last_name field :email embedded_in :school, :inverse_of => :director embedded_in :school, :inverse_of => :students embedded_in :school, :inverse_of => :professors end class School include Mongoid::Document field :name key :name embeds_one :director, :class_name => "User" embeds_many :students, :class_name => "User" embeds_many :professors, :class_name => "User" validates :name, :presence => true end class Task include Mongoid::Document field :name references_one :student, :class => "User" references_one :course end 

In addition, I think that I will use this class to transfer the collection to tasks. The professor creates a TaskCollection and assigns students to it.

  class TaskCollection include Mongoid::Document field :name references_many :students, :stored_as => array, :class => "User" references_one :task end 

So this is my first question. I need some feedback. Is this the right way to use a document-oriented database? Representation? Improvements? Mistakes?

And the second question. How can we integrate this into Devise?

My first attempt:

Routes.rb

devise_for: users ,: path => 'school /: school_id / users'

resources: schools resources: documents ,: only => [: index] resources: tasks,: only => [: index]
end

But when I try to register and the user, I get an error message.

 http://localhost:3000/schools/pablo-de-olavide/users Mongoid::Errors::InvalidCollection in Devise/registrationsController#create Access to the collection for User is not allowed since it is an embedded document, please access a collection from the root document. 

Many thanks for your help.

+6
ruby-on-rails mongodb mongoid devise
source share
2 answers

I just started playing with Devise and Mongoid myself.

Your immediate need is to make User the root document (i.e. not inline), since Devise expects to interact with the user model. I do not know if it is easy to override it to do what you want. Personally, I do not think that would make much sense, even if it could.

Regarding your question about the design of your document, you should take a look at this with Mongdb http://www.mongodb.org/display/DOCS/Schema+Design#SchemaDesign-Embedvs.Reference

In your example, I would consider the User object as a "first class" object that would guarantee its own collection.

+3
source share

Here is a link from the Devise wiki with information on embedding users in another model:

How-To: -Captured users in your model-account-with-Mongoid

+1
source share

All Articles