In my application I have groups which are essentially just tags that can be assigned to different models, through a polymorphic relationship. In model Group I have:
def get_members_of(klass)
self.group_memberships.delete_if {|gm| gm.groupable_type != klass}.map{|g| g.groupable}
end
So if I did:
group.group_memberships
I would get all the objects in group group. However, if I want to limit it to only objects that are instances of model A I would do:
group.get_members_of(A)
In the console this works exactly as it should. However, I get some unexpected behaviour within my controller:
def show
params[:studies1] = @group.get_members_of('Study').length
@studies = @group.get_members_of('Study')
params[:studies2] = @studies.length
@studies = @group.group_memberships.delete_if {|gm| gm.groupable_type != "Study"}.map{|g| g.groupable}
params[:studies3] = @studies.length
end
As expected params[:studies1] == 1, but studies2 and 3 are == 0. I'm sure I'm missing some small thing...