Reputation: 25542
I have the following :has_many, :through
association for my models:
Group Model:
has_many :group_users
has_many :users, :through => :group_users
Users Model:
has_many :group_users
has_many :groups, :before_add => :validates_group, :through => :group_users
GroupsUsers Model:
belongs_to :group
belongs_to :user
In the groups_users
table, I have an additional column that keeps up with the users status (active or inactive). I need to be able to update that column when a user removes them self from a group. How can you use update_attributes
on a join table?
Upvotes: 3
Views: 3379
Reputation: 1952
There's something a little odd about that, so just to be sure--normally you would remove a user from a group by deleting the groups_user record that holds the connection.
If you want that record to persist and just be marked inactive, then you are saying that the user will continue to show up in the group's users collection, and vice versa.
Assuming that is indeed what you want--the record is updateable like any other. You can get to it from either either the user or group side and then edit as usual.
eg, for a user u, group g, groups_user gu
gu = g.groups_users.where('user_id = ?',u.id)
gu.update_attributes (:status => 'inactive')
also, note that if you do this there are probably going to be many times when you want only the active users for a group, or the active groups for a user. See this post for how to do that in this circumstance: Rails has_many :through Find by Extra Attributes in Join Model
Upvotes: 2