Forum: Ruby on Rails has_many :through updates on delete.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Lee I. (Guest)
on 2006-06-08 08:00
(Received via mailing list)
I have a model that looks like this:

class Actor < ActiveRecord::Base
  has_many :member_groups, :foreign_key => 'member_id', :class_name =>
'GroupMember'
  has_many :groups, :through => :member_groups, :source => :group
end

class Group < Actor
  has_many :group_members, :foreign_key => 'group_id',:dependent =>
:destroy
  has_many :members, :through => :group_members, :source => :member
end

class GroupMember < ActiveRecord::Base
  belongs_to :group
  belongs_to :member, :class_name => 'Actor', :foreign_key =>
'member_id'
  belongs_to :source_group, :class_name => 'Group', :foreign_key =>
'source_group_id'
end

Now, when removing a member from a Group (in the method
Group.remove_member), I need to delete a particular GroupMember instance
so I call group_members.delete(group_member).  Fine so far, except that
the "has_many :through" association 'members' doesn't get updated!  It
seems to have retained the cached @members value and doesn't refresh the
association (I can verify that the entry is missing from 'group_members'
and the row has been deleted from the 'group_members' table.  How can I
ensure that 'members' is updated here?  I need to pass those unit tests!
Josh S. (Guest)
on 2006-06-08 10:39
Lee I. wrote:
> Now, when removing a member from a Group (in the method
> Group.remove_member), I need to delete a particular GroupMember instance
> so I call group_members.delete(group_member).  Fine so far, except that
> the "has_many :through" association 'members' doesn't get updated!  It
> seems to have retained the cached @members value and doesn't refresh the
> association (I can verify that the entry is missing from 'group_members'
> and the row has been deleted from the 'group_members' table.  How can I
> ensure that 'members' is updated here?  I need to pass those unit tests!

You can always force any association to reload its cached objects by
sending its accessor with an argument of true.

group.members(true)

Give that a shot.

--
Josh S.
http://blog.hasmanythrough.com
Marcel Molina Jr. (Guest)
on 2006-06-08 10:46
(Received via mailing list)
On Thu, Jun 08, 2006 at 08:39:29AM +0200, Josh S. wrote:
> You can always force any association to reload its cached objects by
> sending its accessor with an argument of true.
>
> group.members(true)
>
> Give that a shot.

It actually doesn't have to be 'true'. Anything that is boolean true
is sufficient. I would recommend using the :reload symbol, as it is more
intention revealing.

  group.members(:reload)

marcel
Lee I. (Guest)
on 2006-06-08 11:20
(Received via mailing list)
Marcel Molina Jr. wrote:
>>> and the row has been deleted from the 'group_members' table.  How can I
> It actually doesn't have to be 'true'. Anything that is boolean true
> is sufficient. I would recommend using the :reload symbol, as it is more
> intention revealing.
>
>   group.members(:reload)
>
>
OK, but shouldn't this "do the right thing?".  These associations are
tied by the :through reference.  I assume that just as a create on the
join table will update both, so should a delete.  Is this a bug?
This topic is locked and can not be replied to.