Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
class Account
include Mongoid::Document
include Mongoid::Timestamps
field :subdomain, :type => String
embeds_many :users
accepts_nested_attributes_for :users
validates_presence_of :name, :subdomain
validates_uniqueness_of :subdomain, :case_sensitive => false
validates_exclusion_of :subdomain, :in => %w(www support media admin help), :message => "%s is reserved"
validates_each :users do |document, attribute, value|
users = value.to_a
unless users.any? && users.all?(&:valid?)
document.errors.add(attribute, :invalid, :value => value)
end
end
end
class User
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :account, :inverse_of => :users
devise :database_authenticatable, :recoverable, :rememberable,
:authentication_keys => [ :email, :subdomain ]
def self.find(*args)
options = args.extract_options!
user_options = Hash[*(options[:conditions] || {}).map { |k, v| [ :"users.#{k == :id ? :_id : k}", v ] }.flatten]
if account = Account.find(*(args + [options.merge(:conditions => user_options)]))
account.users.detect do |u|
options[:conditions].all? { |k, v| u.send(k) == v }
end
else
super
end
end
def self.find_for_authentication(conditions={})
if account = Account.where(:subdomain => conditions.delete(:subdomain)).first
account.users.detect { |u| u.email == conditions[:email] }
else
nil
end
end
end
@nstielau

This comment has been minimized.

Show comment Hide comment
@nstielau

nstielau Oct 5, 2010

This doesn't really explain how users are created and embedded in the accounts. "Access to the collection for User is not allowed since it is an embedded document, please access a collection from the root document." I'll fork and add anything I come up with. Thanks for the start.

nstielau commented Oct 5, 2010

This doesn't really explain how users are created and embedded in the accounts. "Access to the collection for User is not allowed since it is an embedded document, please access a collection from the root document." I'll fork and add anything I come up with. Thanks for the start.

@mrichman

This comment has been minimized.

Show comment Hide comment
@mrichman

mrichman Jan 2, 2011

What does routes.rb look like here? What would an example of seeds.rb look like when creating a single account with a single dummy user? Thanks!

mrichman commented Jan 2, 2011

What does routes.rb look like here? What would an example of seeds.rb look like when creating a single account with a single dummy user? Thanks!

@djfobbz

This comment has been minimized.

Show comment Hide comment
@djfobbz

djfobbz Feb 14, 2011

with this approach, how much users (maximum) can i embed in my account document? unlimited? would references many be a better approach?

djfobbz commented Feb 14, 2011

with this approach, how much users (maximum) can i embed in my account document? unlimited? would references many be a better approach?

@fauxparse

This comment has been minimized.

Show comment Hide comment
@fauxparse

fauxparse Feb 15, 2011

@djfobbz: I'm not in a position to answer questions about your problem domain. This was a good fit for me, since in my app I always had access to the current account and so all the user stuff was scoped to that account and loaded automatically, and there weren't many users per account.Your mileage may vary.

Owner

fauxparse commented Feb 15, 2011

@djfobbz: I'm not in a position to answer questions about your problem domain. This was a good fit for me, since in my app I always had access to the current account and so all the user stuff was scoped to that account and loaded automatically, and there weren't many users per account.Your mileage may vary.

@jeremy6d

This comment has been minimized.

Show comment Hide comment
@jeremy6d

jeremy6d Jan 4, 2013

I've documented an alternative approach here. I'd be interested in any comments, criticisms, suggestions, etc.

jeremy6d commented Jan 4, 2013

I've documented an alternative approach here. I'd be interested in any comments, criticisms, suggestions, etc.

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