Parametrized join in Rails 4

With sanitize_sql_array, that would be:

# Warning: sanitize_sql_array is a protected class method, be aware of that to properly use it in your code
ar = ["LEFT OUTER JOIN bars ON foos.id = bars.foo_id AND bars.baz = %s", baz]

# Within a class method in foo model:
sanitized_sql = sanitize_sql_array(ar)
Foo.joins(sanitized_sql)

Tried it and it worked.


Active record models have sanitize class method, so you could do:

Foo.joins("LEFT OUTER JOIN bars ON foos.id = bars.foo_id AND bars.baz = #{Foo.sanitize(baz)}")

__

It's been removed starting rails 5.1, use ActiveRecord::Base.connection.quote() instead