Skip to content

Instantly share code, notes, and snippets.

@macasek
Created November 9, 2012 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macasek/4047923 to your computer and use it in GitHub Desktop.
Save macasek/4047923 to your computer and use it in GitHub Desktop.
HBTM relationship validation question
# Basically I am building a multi-domain/tenant app and in some cases I need to specify what users are valid on what domains. A dumbed down contrived example would be:
class User < ActiveRecord::Base
has_and_belongs_to_many :domains
end
class Domain < ActiveRecord::Base
has_and_belongs_to_many :users
end
u = User.first
# this autosaves the relationship and no validators/callbacks on User or Domain are invoked.
u.domains << Domain.first
# For the domains relationship on the user I want to validate that it is never empty, and that the current domain the user is accessing is never removed. There is also a possibility that I would never want any previously added domain to be removed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment