AR Joins support for parameterized conditions

Is there a fundamental reason that AR.joins() does not support parameterized conditions? I fully expected this to work

Document.
   joins("LEFT JOIN `votes` ON `votes`.`v_id` = `document`.`id` AND `votes`.`giver_id` = ?", user_id).
   where('document.creator_id = ?', user_id, .....).
   select('votes.*', `document.param1')

Then I read the docs :slight_smile: By forcing the join where clause to the outer where, have to handle NULL:

Document.
   joins("LEFT JOIN `votes` ON `votes`.`v_id` = `document`.`id`").
   where('document.creator_id = ? AND (`votes`.`giver_id` = ? OR `votes`.`giver_id` IS NULL)', user_id, user_id).
   select('votes.*', `document.param1')

So, before I investigated a PR, wanted to see if this had been considered and passed on for whatever reason.

Thank you for your feedback.

Mark

Hi Mark,

Unfortunately the use of multiple arguments to joins() is already taken - to pass multiple different joins.

I think this was particularly intended for the case where the arguments to joins() are association names -

Document.joins(:authors, :votes)

But unfortunately it is also supported for SQL strings

Document.joins(“JOIN tableA ON foo”, “JOIN tableB on bar”)

I personally feel that we could live without that for the string joins case, because of course you could always just combine the two strings together and achieve the same effect.

But it is a backwards-incompatible change, which always makes it a bit harder to get through, and the code may be a little ugly since you still need to interpret multiple symbols in a different way for the other case.

Personally I’d actually like to see the use of scopes here instead. To avoid the above argument dilemma, we could provide them using a block to the joins() method:

Document.

joins(:votes) { where(“extra join conditions to add to the ON clause go here”) }.

where(…).

to_a

For a standard join this is no different to merging the scope into the query relation, so we wouldn’t bother just for that.

But, we could then add a left_joins() method, and that would then solve exactly the case you described - but it would also allow us to reuse existing associations and scopes without having to drop down to manually writing out SQL. That would be a huge win for me!

Will

Hi,

Will Bryant writes:

But unfortunately it is also supported for SQL strings

  Document.joins("JOIN tableA ON foo", "JOIN tableB on bar")

Why not keep with the array syntax used in other places?

    Document.joins(["JOIN tableA ON foo = ?", myvar], "JOIN tableB on bar")

We support such syntax in other places as well.

Regards,

Hi Will,

Hi Will,