Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

How to have join with additional ON clauses #397

Open
ioquatix opened this issue Aug 25, 2015 · 2 comments
Open

How to have join with additional ON clauses #397

ioquatix opened this issue Aug 25, 2015 · 2 comments

Comments

@ioquatix
Copy link

Hi,

I'd like to generate a query like:

SELECT * FROM user LEFT OUTER JOIN user_deal_notification ON user_deal_notification.user_id == user.id AND user_deal_notification.deal_id = 1234 WHERE user_deal_notification.id IS NOT NULL

How do I do this with Squeel?

@ioquatix
Copy link
Author

I feel like something along the lines of:

User.joins{user_deal_notifications.on{user_deal_notifications.deal_id == 10721}.outer}.where{user_deal_notification.id != nil}

should work.. but it complains Association named 'on' was not found

@ioquatix
Copy link
Author

So.. I tried using the subquery stuff..

                            subquery = UserDealNotification.where(deal: self)

            users = users.joins{subquery.as(:user_deal_notification).on{user.id == user_deal_notification.user_id}.outer}.where{
                (user_deal_notification.id != nil) & (user_deal_notification.deal_id == self.id)
            }

But when I try executing count on this, AR doesn't like it:

d.users_within_range(ignore_notified: true).count
TypeError: Cannot visit Arel::SelectManager

Some googling told me that ARel doesn't like subqueries?

I'd really prefer if there was some easy way to just specify additional ON conditions.. I'm going to revert to using a join(string) which is slightly disappointing.

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

No branches or pull requests

1 participant