How to leave to join Arel?

Arel 3.0.2 provides two classes for specifying the type of connection: Arel::Nodes::InnerJoinand Arel::Nodes::OuterJoinand uses InnerJointhe default.

foo = Arel::Table.new('foo')
bar = Arel::Table.new('bar')

foo.join(bar, Arel::Nodes::InnerJoin) # inner
foo.join(bar, Arel::Nodes::OuterJoin) # outer

foo.join(bar, ???) # left

How can you join two tables if you want to create a left join?

+5
source share
2 answers

Using

foo.join(bar, Arel::Nodes::OuterJoin) # outer

Because LEFT JOIN = LEFT OUTER JOIN. Outer are options. See here

+8
source

Here is a complete example of rail models for those who want to see them.

It took me a couple of hours to understand, so I decided to share with the whole world

This assumes that you have one model with a name RssFeedand another with a name RssFeedUserthat is the connection model for has_many: through for the model with the nameUser

RssFeed.find_by_sql(
  RssFeed
    .arel_table
    .join(RssFeedUser.arel_table, Arel::Nodes::OuterJoin)
    .on(RssFeed.arel_table[:id].eq(RssFeedUser.arel_table[:rss_feed_id]))
    .where(RssFeedUser.arel_table[:user_id].eq(nil))
    .project('rss_feeds.*')
)
0
source

All Articles