Skip to content

Instantly share code, notes, and snippets.

@swarut
Created October 3, 2012 05:59
Show Gist options
  • Save swarut/3825305 to your computer and use it in GitHub Desktop.
Save swarut/3825305 to your computer and use it in GitHub Desktop.
Rails : A proper validation #rails #validation
def appropriate_visibilities
unless (self.visibilities - self.user.roles).empty?
errors.add(:visibilities, "invalid visibilities assignment")
end
# Below is a bad one, change the visibilities silently. this is not a right behavior of validation
# self[:visibilities] = BIT_MASKER.mask(
# # This is assuming that self.visibilities, at least, must have :regular as its member
# # or we will end up getting visibilities = [].
# # See the comment on visibilities() above
# self.visibilities.reject{ |role| !self.user.roles.include?(role) && role != :regular }
# )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment