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


> On 11/12/2014, at 05:00 , digger69 <m...@nadigs.net> wrote:
> 
> 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 :)  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
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to rubyonrails-core+unsubscr...@googlegroups.com 
> <mailto:rubyonrails-core+unsubscr...@googlegroups.com>.
> To post to this group, send email to rubyonrails-core@googlegroups.com 
> <mailto:rubyonrails-core@googlegroups.com>.
> Visit this group at http://groups.google.com/group/rubyonrails-core 
> <http://groups.google.com/group/rubyonrails-core>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to