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!
Lee Iverson 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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
On Thu, Jun 08, 2006 at 08:39:29AM +0200, Josh Susser wrote:> Lee Iverson 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.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 -- Marcel Molina Jr. <marcel@vernix.org>
Marcel Molina Jr. wrote:> On Thu, Jun 08, 2006 at 08:39:29AM +0200, Josh Susser wrote: > >> Lee Iverson 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. >> > > 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?