active_record_union icon indicating copy to clipboard operation
active_record_union copied to clipboard

Cannot union relation with includes

Open c80609a opened this issue 7 years ago • 6 comments

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

c80609a avatar Aug 09 '16 05:08 c80609a

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 avatar Aug 09 '16 13:08 brianhempel

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

brianhempel avatar Aug 13 '16 16:08 brianhempel

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

pynixwang avatar Oct 26 '16 15:10 pynixwang

Can you provide more detailed information?

brianhempel avatar Oct 26 '16 20:10 brianhempel

@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!

oyeanuj avatar Dec 10 '16 20:12 oyeanuj

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

brianhempel avatar Dec 10 '16 22:12 brianhempel