So I think I've hit sort of a wall on the comprehension of how to do this nested "has_many through". Basically a User will have many roles and organizations. So right now I have 4 tables, users, roles, organizations and memberships (JOIN with user_id, organization_id and role_id).
So far this is what I have...but am having issue comprehending how to assign say a role to a user and how to correctly call and assign things through the association. I also feel like I'm missing something in the Organization and Role models.
# app/models/user.rb
strategy :many_roles, :role_class => 'Role', :user_roles_class => 'Membership'
# strategy creates these two relationships
# has_many :memberships
# has_many :many_roles, :through => :memberships, :source => :roles
valid_roles_are :admin, :vote_counter, :voter
has_many :organizations, :through => :memberships
# app/models/role.rb
has_many :memberships
has_many :users, :through => :memberships
# app/models/organization.rb
has_many :memberships
has_many :users, :through => :memberships
# app/models/membership.rb
belongs_to :organization
belongs_to :user
belongs_to :role
validates_uniqueness_of :user, :scope => [:role, :organization]
Is this still within the range of capabilities of role_active_record, and if so would you happen to have some examples of how to assign / query the role?
I've tried this for example...while it works I have no idea if that's really how it should be done or how to implement that in my controllers from form submissions
1.9.2-p290 :068 > user = User.find(2)
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
=> #<User id: 2, fullname: "Trey Dockendorf", username: "treydock", role: "admin", email: "
trey...@gmail.com", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 25, current_sign_in_at: "2012-04-27 16:11:42", last_sign_in_at: "2012-04-20 19:34:36", current_sign_in_ip: "165.91.11.204", last_sign_in_ip: "128.194.198.102", created_at: "2012-02-06 03:33:12", updated_at: "2012-04-27 16:11:42">
1.9.2-p290 :069 > membership = user.memberships.find(:first, :conditions => ['organization_id = ?', 3])
Membership Load (0.2ms) SELECT `memberships`.* FROM `memberships` WHERE `memberships`.`user_id` = 2 AND (organization_id = 3) LIMIT 1
=> #<Membership organization_id: 3, user_id: 2, role_id: nil>
1.9.2-p290 :070 > membership.role
=> nil
1.9.2-p290 :071 > admin_role = Role.find_by_name('admin')
Role Load (0.5ms) SELECT `roles`.* FROM `roles` WHERE `roles`.`name` = 'admin' LIMIT 1
=> #<Role id: 1, name: "admin", created_at: "2012-04-30 00:26:05", updated_at: "2012-04-30 00:26:05">
1.9.2-p290 :072 > membership.role = admin_role
=> #<Role id: 1, name: "admin", created_at: "2012-04-30 00:26:05", updated_at: "2012-04-30 00:26:05">
1.9.2-p290 :073 > membership.role.save!
(0.3ms) BEGIN
(13.4ms) SELECT 1 FROM `roles` WHERE (`roles`.`name` = BINARY 'admin' AND `roles`.`id` != 1) LIMIT 1
(0.1ms) COMMIT
=> true
1.9.2-p290 :074 > membership
=> #<Membership organization_id: 3, user_id: 2, role_id: 1>
Thanks
- Trey