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.

Arel/AR prepending 'AND' unconditionally after newlines in string literals in RC2

#5457

Not sure if that title makes sense, but an easy way to reproduce it is:

SomeModel.where("varchar_test IS NULL\nor varchar_test IS NOT NULL")

Without the newline I get:

"SELECT mysql2_test.* FROM mysql2_test WHERE (varchar_test IS NULL or varchar_test IS NOT NULL)"

With the newline I get:

"SELECT mysql2_test.* FROM mysql2_test WHERE (varchar_test IS NULL\n AND or varchar_test IS NOT NULL)"

Which is invalid.

Trying to track down where this is happening, if I find it I'll try to come up with a patch but I figure you'd have a better idea of where it's happening.

Reported by Brian Lopez · August 25th, 2010 @ 05:57 PM

State: resolved
Milestone: 3.0.2
Assigned to: Aaron Patterson Aaron Patterson
Importance: High

Activity

  1. Brian Lopez
    Brian Lopez
    • Tag changed from pink warrior rc2 arel to pink warrior, arel, rc2

    August 25th, 2010 @ 05:59 PM

  2. José Valim
    José Valim
    • Milestone cleared.
    • State changed from new to open
    • Importance changed from to High

    August 25th, 2010 @ 06:01 PM

  3. Dim
    Dim

    Got it, I found the issue!

    In http://github.com/rails/arel/blob/master/lib/arel/algebra/relations... the clause is passed to Array(clause), therefore:

     > Array(%("table"."some_column" = 'some\nvalue'))
    => ["\"table\".\"some_column\" = 'some\n", "value'"]
    

    August 25th, 2010 @ 06:13 PM

  4. Brian Lopez
    Brian Lopez

    Wow, didn't realize Array() would treat newlines like that...

    August 25th, 2010 @ 06:21 PM

  5. José Valim
    José Valim

    Yup, it does! This is why in Rails we usually use Array.wrap.

    August 25th, 2010 @ 06:27 PM

  6. Brian Lopez
  7. Dim
    Dim

    Just to confirm:

    ruby-1.9.2-rc2 > Array("a\nb")
     => ["a\nb"] 
    ree-1.8.7-2010.02 > Array("a\nb")
     => ["a\n", "b"]
    

    August 25th, 2010 @ 06:29 PM

  8. Xavier Noria
  9. Dim
    Dim

    Or just go with [clause].flatten (not to duplicate code for just one single Array() instance)

    August 25th, 2010 @ 06:52 PM

  10. Brian Lopez
    Brian Lopez

    If I remember correctly, Aaron made this change for performance reasons so whatever solution we end up figuring out should hopefully be performant; if at all possible.

    August 25th, 2010 @ 07:01 PM

  11. Xavier Noria
    Xavier Noria

    Yeah I was in a hurry before. I mean, Array() has that gotcha and AS implements Array.wrap for that reason. If it makes sense it's there. If there's only one place where it is used then some simple Ruby may be better. (But the next guy may wonder why Array() wasn't used, so perhaps with a comment?)

    August 25th, 2010 @ 07:18 PM

  12. Aaron Patterson
    Aaron Patterson

    I'm on it. I'll have a fix for this later today. I'm super jet lagged right now. :'(

    August 25th, 2010 @ 10:10 PM

  13. Aaron Patterson
  14. Jeremy Kemper