public
Last active

mongo stuff

  • Download Gist
mongo_test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
require 'mongoid'
 
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db("so_test")
end
 
class Client
include Mongoid::Document
belongs_to :contact
field :name, type: String
end
 
class Contact
include Mongoid::Document
has_many :clients
field :name, type: String
end
 
Contact.create(:name => "Bill")
jill = Contact.create(:name => "Jill")
jill.clients.create(:name => "Steve")
 
p "Has Clients"
Contact.any_in(_id: Client.all.distinct("contact_id")).each do |c|
p c
end
p "No Clients"
Contact.not_in(_id: Client.all.distinct("contact_id")).each do |c|
p c
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.