D For A Web Developer

via Digitalmars-d digitalmars-d at puremagic.com
Thu May 1 06:54:48 PDT 2014


On Thursday, 1 May 2014 at 10:44:42 UTC, Jacob Carlborg wrote:
> On 2014-04-30 22:38, Adam D. Ruppe wrote:
>> I also find myself really missing outer joins and views.
>
> For outer joins:
>
> 1. You can always use raw SQL, also in combination with 
> ActiveRecord
>
> Post.joins(:comments).joins("outer join foos on foos.post_id = 
> posts.id").where(title: "bar")
>
> 2. My preferred choice, using Squeel [1] (another gem). With 
> Squeel the above would look like this:
>
> Post.joins(:comments).joins{ foos.outer }.where(title: "bar")

You can also use the built-in `includes()`, which does a LEFT 
OUTER JOIN:

     Post.includes(:comments).where(comments: {title: "bar"})

(It also eager-loads the comments, but this is usually desired 
anyway, because an OUTER JOIN doesn't make sense if you're not 
going to use the joined records.)


More information about the Digitalmars-d mailing list