Created
July 1, 2012 23:05
-
-
Save voltechs/3029968 to your computer and use it in GitHub Desktop.
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
class Membership < ActiveRecord::Base | |
# attr_accessible :title, :body | |
set_primary_keys :user_id, :group_id | |
belongs_to :user | |
belongs_to :group | |
# Weird that we have to say "belongs_to", but it works. | |
belongs_to :role | |
validates_presence_of :user, :group | |
validates_uniqueness_of :user_id, :scope => :group_id | |
end |
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
class Role < ActiveRecord::Base | |
has_many :memberships | |
scope :in_group, lambda { |g| joins(:memberships).where(:group_ids => g.ancestors ) } | |
# scope :groups joins(:memberships).where(:group_ids => [array_of_group_ids]) | |
end |
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
(SELECT `roles`.* FROM `roles` INNER JOIN `memberships` `memberships_roles` ON `memberships_roles`.`role_id` = `roles`.`id` INNER JOIN `memberships` ON `roles`.`id` = `memberships`.`role_id` WHERE `memberships`.`user_id` = 1160 AND `roles`.`group_ids` IN ('HHC', 'NUCC') AND (expiration > '2012-07-01')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment