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:
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
Many thanks for your help.