brianhempel / active_record_union

UNIONs in ActiveRecord! Adds proper union and union_all methods to ActiveRecord::Relation.
Other
423 stars 41 forks source link

Cannot union relation with includes #11

Open c80609a opened 8 years ago

c80609a commented 8 years ago

I get an error "Cannot union relation with includes" running my app (key gems: activeadmin + active_record_union). But the same code works without any errors running via console.

App:

#...
def self.all_except_busy_alien(admin_user)
        not_my_free_areas = self.free_areas
                         .joins(:property)
                         .where.not(:properties => {assigned_person_id: admin_user.id})

        all_my_areas = self.joins(:property)
                    .where(:properties => {assigned_person_id: admin_user.id})

        all_my_areas.union(not_my_free_areas)
end
#...

Console:

Loading development environment (Rails 4.2.0)

>> all_my_areas = Area.joins(:property).where(:properties => {assigned_person_id: 6})
... Area Load (174.8ms)  SELECT `areas`.* FROM ...
#<ActiveRecord::Relation

>> all_my_areas.count
115

>> not_my_free_areas = Area.free_areas.joins(:property).where.not(:properties => {assigned_person_id: 6})
... Area Load (0.4ms)  SELECT `areas`.* FROM ...
#<ActiveRecord::Relation

>> not_my_free_areas.count
65

>> r = all_my_areas.union(not_my_free_areas)
... SELECT `areas`.* FROM ((SELECT `areas`.* FROM `areas` INNER JOIN `properties` ON `properties`.`id` = `areas`.`property_id` WHERE `properties`.`assigned_person_id` = 6) UNION (SELECT `areas`.* FROM `areas` INNER JOIN `areas_astatuses` ON `areas_astatuses`.`area_id` = `areas`.`id` INNER JOIN `astatuses` ON `astatuses`.`id` = `areas_astatuses`.`astatus_id` INNER JOIN `properties` ON `properties`.`id` = `areas`.`property_id` WHERE `astatuses`.`tag` = 'free' AND (`properties`.`assigned_person_id` != 6))) `areas`;
#<ActiveRecord::Relation

>> r.count
180
brianhempel commented 8 years ago

The relevant code in this gem is here: https://github.com/brianhempel/active_record_union/blob/master/lib/active_record_union/active_record/relation/union.rb#L50-L54

So see what not_my_free_areas.includes_values is and what all_my_areas.includes_values is and that should give you a clue to what's going on.

brianhempel commented 8 years ago

@c80609a did you gain any more information about what's going on in your app?

pynixwang commented 7 years ago

I run into this issues because I include properties in activeadmin controller

brianhempel commented 7 years ago

Can you provide more detailed information?

oyeanuj commented 7 years ago

@brianhempel Not sure if this is the right issue to comment upon, but am coming from the Readme where you asked folks to make a noise around supporting 'eager-loading'. Is that still on the radar? In my case, almost all scopes have eager-loading and includes, and I'd love to clean up my code using this!

brianhempel commented 7 years ago

@oyeanuj Can you quick open a separate issue for that? The problem that spawned this particular thread is unclear.