Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Problem with find with :joins and :include in same call

#2631

In a model I have the following named scope: named_scope :valid, {:include => :broker, :joins => 'INNER JOIN payers ON payers.customer_id = brokers.id AND payers.customer_type = 'Broker'', :conditions => "payers.status = 'active' and ticket_sets.delete_flag = 0 and ticket_sets.qty > 0 and (brokers.ei_id is NULL or (brokers.ei_id is not null and ticket_sets.ei_id is not null)) "}

This worked perfectly in Rails 2.2.2, but in Rails 2.3.2 this blows up.

Looking at the log file, it appears that the :include clause isn't being parsed.

SELECT ticket_sets.* FROM ticket_sets INNER JOIN payers ON payers.customer_id = brokers.id AND payers.customer_type = 'Broker' WHERE (payers.status = 'active' and ticket_sets.delete_flag = 0 and ticket_sets.qty > 0 and (brokers.ei_id is NULL or (brokers.ei_id is not null and ticket_sets.ei_id is not null)) )

If I remove the the joins clause, the brokers get included like they should be, but it doesn't have payers referenced.

Reported by Andrew Selder · May 10th, 2009 @ 08:47 PM

State: stale
Milestone: 3.x
Assigned to: Frederick Cheung Frederick Cheung
Importance: none

Activity

  1. Frederick Cheung
    Frederick Cheung
    • Assigned user set to Frederick Cheung

    May 10th, 2009 @ 09:00 PM

  2. mueller182 (at gmail)
    mueller182 (at gmail)

    Well, the same problem happens with a find using :include and :joins

    May 15th, 2009 @ 01:36 PM

  3. James Le Cuirot
    James Le Cuirot

    Ugh I'm behind on my project, the end finally looks near and then I run into this, with no workaround that I can see. :( Any ideas yet? I'm gonna start digging now.

    September 13th, 2009 @ 09:49 PM

  4. James Le Cuirot
    James Le Cuirot

    I've figured it out. It was caused by the fix for ticket #528 Check joins when determining preloading of includes. This fix tries to determine which tables you have explicitly joined so that you can force preloading (instead of regular eager loading) if you want to. The problem is that it does a bad job of this since it will also pick up tables named in the join condition. In my case and the case above, this table has to be explicitly named to prevent ambiguity. As far as I can see, it can only be resolved by replacing the somewhat nasty regex with an even nastier one.

    /([.a-zA-Z_]+).?./

    Becomes...

    /\bJOIN\s+["]?([\.a-zA-Z_]+)["]?\s/i

    This could possibly be improved. Here's a patch against 2-3-stable anyway. I may add tests and a patch for master later. Really tired right now. :(

    September 14th, 2009 @ 12:11 AM

  5. Frederick Cheung
    Frederick Cheung

    Does someone familiar with several databases want to comment on whether joins always look like this (seems to me like they do but databases never cease to surprise me)? I seem to recall the some database uses [] to quote table names - i think the existing regexes just use . as a catchall

    Other than that looks fine.

    Separate from this, you should avoid all this if you use the form of joins where you name associations (since then rails doesn't have to parse a blob of sql to work out what is being joined

    September 14th, 2009 @ 10:51 AM

  6. James Le Cuirot
    James Le Cuirot

    I'm using PostgreSQL right now and I used MySQL before but those are the obvious cases. I checked the existing quote column/table methods and only found " and . Most databases don't force you to quote anyway though, right?

    I know you can name the associations but in my case, I was actually joining a sub-select.

    September 14th, 2009 @ 06:47 PM

  7. Frederick Cheung
    Frederick Cheung

    They don't force you but you don't want it to break if you do choose to, which I believe why the other regexps just use . for the quoting character (if my memory of bugs past are correct)

    September 14th, 2009 @ 06:50 PM

  8. James Le Cuirot
    James Le Cuirot

    Sorry, the markup is confusing my comments slightly. I meant " and backticks.

    What I should have said was that just . doesn't work for sub-selects before something like "LEFT OUTER JOIN (SELECT ..." will return SELECT as a table name. I considered [^(] but I think I dismissed it for some reason.

    September 14th, 2009 @ 07:01 PM

  9. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  10. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to stale
    • Importance changed from to

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

    October 9th, 2010 @ 04:14 AM

  11. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 9th, 2010 @ 10:05 PM

  12. Jeff Kreeftmeijer