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
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!
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.
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.
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.