Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot union relation with includes #11

Open
c80609a opened this issue Aug 9, 2016 · 6 comments
Open

Cannot union relation with includes #11

c80609a opened this issue Aug 9, 2016 · 6 comments

Comments

@c80609a
Copy link

c80609a commented Aug 9, 2016

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

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

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

@pynixwang
Copy link

pynixwang commented Oct 26, 2016

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

@brianhempel
Copy link
Owner

Can you provide more detailed information?

@oyeanuj
Copy link
Contributor

oyeanuj commented Dec 10, 2016

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants