Skip to content

Fix member association filtering#48

Open
dwbutler wants to merge 2 commits intomasterfrom
fix/member-association-filtering
Open

Fix member association filtering#48
dwbutler wants to merge 2 commits intomasterfrom
fix/member-association-filtering

Conversation

@dwbutler
Copy link
Owner

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.

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 dwbutler added the bug label Jun 15, 2016
@dwbutler
Copy link
Owner Author

Fixes issue reported in #47.

@dwbutler
Copy link
Owner Author

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

@juhazi
Copy link

juhazi commented Jun 27, 2016

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
Copy link
Owner Author

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
Copy link
Collaborator

joelvh commented Aug 4, 2017

@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
Copy link
Owner Author

dwbutler commented Aug 4, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants