dwbutler / groupify

Add group and membership functionality to your Rails models
MIT License
194 stars 42 forks source link

Fix member association filtering #48

Open dwbutler opened 8 years ago

dwbutler commented 8 years ago

When defining an association through a join table to a model using single table inheritance, ActiveRecord will always store the base class name in the join table. This allows the base class to always be correctly queried, but makes it challenging to correctly define an association that only returns a subclass.

Groupify groups define member associations using the source_type set correctly to the base class, but this returns all members of the base class instead of filtering to only members of the subclass. This fixes the issue by adding a filtering condition to the association on the type column, but only if STI is detected.

dwbutler commented 8 years ago

Fixes issue reported in #47.

dwbutler commented 8 years ago

I'll need to do some more research on how to accomplish this in Mongoid before this can be released.

juhazi commented 8 years ago

Mongoid has_many does not seem to support a filter for the relation.

You could hack around by not creating a new has_many but using the has_many :groups relation with a where filter like outlined in http://rohitrox.github.io/2013/08/30/single-table-inheritance-in-mongoid/

Something like

def organizations
  groups.where(_type: "Organization")
end

Alternatively has_members could be downgraded to AR only status temporarily as they do not even currently function intuitively in Mongoid?

dwbutler commented 8 years ago

I would be okay with having documented differences between Mongoid and ActiveRecord. It probably isn't possible to maintain 100% feature parity, but they can retain the 99% the same basic interface. The Readme should also be more insistent that consumers stick to the public interface and not rely too much on the internal implementation details.

joelvh commented 7 years ago

@dwbutler I think this is addressed with the new has_group helper and STI work in #61, which lets you specify a :class_name option. Consider closing?

dwbutler commented 7 years ago

Yes I agree. This fix won't be necessary once the has_group helper is released.