Created
April 5, 2012 11:55
-
-
Save sj26/2310315 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 User < ActiveRecord::Base | |
attr_accessible :name | |
has_many :admin_memberships, class_name: "Membership", conditions: {role: 'admin'} | |
has_many :editor_memberships, class_name: "Membership", conditions: {role: 'editor'} | |
def admin? | |
admin_memberships.exists? | |
end | |
def editor? | |
editor_memberships.exists? | |
end | |
end | |
class Group < ActiveRecord::Base | |
attr_accessible :name | |
has_many :admin_memberships, class_name: "Membership", conditions: {role: "admin"} | |
has_many :editor_memberships, class_name: "Membership", conditions: {role: "editor"} | |
has_many :admins, through: :admin_memberships, source: :user | |
has_many :editors, through: :editor_memberships, source: :user | |
end | |
class Membership < ActiveRecord::Base | |
attr_accessible :role | |
belongs_to :user | |
belongs_to :group | |
end | |
sam = User.new name: "Sam" | |
# => #<User id: 1, name: "Sam", created_at: "2012-04-05 11:45:54", updated_at: "2012-04-05 11:45:54"> | |
rainbows = Group.new name: "Rainbows" | |
# => #<Group id: 1, name: "Rainbows", created_at: "2012-04-05 11:46:06", updated_at: "2012-04-05 11:46:06"> | |
rainbows.admins << sam | |
# => [#<User id: 1, name: "Sam", created_at: "2012-04-05 11:45:54", updated_at: "2012-04-05 11:45:54">] | |
rainbows.admin_memberships | |
# => [#<Membership id: 2, user_id: 1, group_id: 1, role: "admin", created_at: "2012-04-05 11:48:58", updated_at: "2012-04-05 11:48:58">] | |
user.admin? | |
# Membership Exists (0.3ms) SELECT 1 FROM "memberships" WHERE "memberships"."user_id" = 1 AND "memberships"."role" = 'admin' LIMIT 1 | |
# => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice. Note that the
conditions
syntax is very different in Rails 4http://guides.rubyonrails.org/association_basics.html#the-has-many-association
And if you (like me) are tempted to use Rails 4.1 enums, the syntax is a bit more interesting pending the outcome of these:
rails/rails#13918
rails/rails#13433
In my case, I have the
role
in theMembership
table (soUsers
can have a different role perGroup
) and here is what I'm using ingem 'rails', '4.1.0.beta1'
Here is the enum declaration