Created
November 9, 2012 20:11
-
-
Save macasek/4047923 to your computer and use it in GitHub Desktop.
HBTM relationship validation question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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