Note that there are some explanatory texts on larger screens.

plurals
  1. POMany to many persistence ruby mongoid
    primarykey
    data
    text
    <p>Hey this is my first post so tell me if I am not giving you enough information</p> <p>So, I am trying out ruby1.9.2 and using mongoid2.2.0 with mongodb1.8.2 and I am having trouble persisting database documents in the code with a many to many relationship.</p> <pre><code>require 'mongoid' require 'mongo' Mongoid.load!("../Configurations/mongoid.yml") Mongoid.configure do |config| config.master = Mongo::Connection.new.db("godfather") end connection = Mongo::Connection.new connection.drop_database("godfather") database = connection.db("godfather") class Project include Mongoid::Document field :name, type: String key :name field :numPeople, type: Integer has_and_belongs_to_many :people end class Person include Mongoid::Document field :name, type: String key :name field :numProjects, type: Integer, default: 0 has_and_belongs_to_many :projects def add_project(project_name) project = Project.create(name: project_name) self.numProjects = self.numProjects + 1 self.projects &lt;&lt; project self.save end def has_project?(project_name) self.projects.each do |project| if project.name.upcase == project_name.upcase return true end end return false end end database = Project.create(name: "Database") alice = Person.create(name: "Alice") alice.add_project("Database") puts "has project? #{alice.has_project?("Database")}"#outputs true puts "Alice has #{alice.numProjects} projects"#outputs 1 puts "Alice really has #{alice.projects.size} projects"#outputs 1 editor = Project.create(name: "Editor") john = Person.create(name: "John") john.has_project?("Editor") john.add_project("Editor") puts "has project? #{john.has_project?("Editor")}"#outputs false puts "John has #{john.numProjects} projects"#outputs 1 puts "John really has #{john.projects.size} projects"#outputs 0 operatingSystem = Project.create(name: "OperatingSystem") drinking = Project.create(name: "Drinking") henry = Person.create(name: "Henry") henry.add_project("OperatingSystem") henry.has_project?("OperatingSystem") henry.add_project("drinking") henry.add_project("Editor") puts "Henry has #{henry.numProjects} projects"#outputs 3 puts "Henry really has #{henry.projects.size} projects"# outputs 3 </code></pre> <p>So the problem is John outputs that he has 0 projects when I have added one. </p> <p>The weird thing is when I use the mongo terminal to inspect the database it shows john having the one project "Editor" which the code did not find in john.projects.size. </p> <p>Ideally the order in which i call add_project and has_project? should not affect the code and matter.</p> <ul> <li>thanks</li> </ul>
    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.
    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