Adding support to query all subclasses when using sci #275

Merged
merged 1 commit into from May 25, 2011

4 participants

@kelsin

My first pass at this a moment ago didn't realize that ActiveSupport includes a descendant tracker. This version now uses that.

Original pull request message:

Howdy. At my company we are looking into switching from mongoid -> mongo mapper and so far the conversion is going great. The biggest issue is the lack of this feature.

We have a class hierarchy like this:
TradeCoupon < Coupon < Offer

We would like to be able to query on Coupon and get TradeCoupons and right now the sci plugin doesn't support this. This is my first attempt and solving this. It works for our code and passes the test suite (including my additions to test querying). I'm not sure if I followed style perfectly or anything like that so let me know if I should change anything.

This does change behavior and will break people's code if they are in the same situation and expect querying Coupons to NOT bring in any TradeCoupons so that is a consideration.

Thanks :)

@jnunemaker

I am not sure about this. Does AR or any other object mapper behave this way?

@kelsin

Mongoid works like this (like I said we're trying to move from Mongoid to MM).

AR works like this as well: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb#L1010

Logically (going by my original example) you want TradeCoupons and Coupons to show up when you query Offers (this works currently). Both Coupons and TradeCoupons are offers so it makes sense. When you query Coupons you want TradeCoupons to show up for the same reason and currently it does not.

We're going through a lot of code changes in our switch of course, and we're not opposed to changing our code to make the move, but when looking at this change it felt like it was a behavior that should be added to MM, not worked around in our code.

@bkeepers bkeepers merged commit 314fb8a into mongomapper:master May 25, 2011
@cmeiklejohn

Nice!

@kelsin

Thanks :)

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