Skip to content

Instantly share code, notes, and snippets.

@lengarvey
Created March 10, 2012 08:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lengarvey/2010817 to your computer and use it in GitHub Desktop.
Save lengarvey/2010817 to your computer and use it in GitHub Desktop.
mongo stuff
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
@araslanov-e
Copy link

Is it a good way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment