Hey Mike,

You need to use the alias method and tell it what to call the alias.
So, you'll want something like this:

service_table = Arel::Table.new(:service)
s1 = service_table.alias("s1")
s2 = service_table.alias("s2")

Take a look at the inner query of this (relevant_contents_query):
https://gist.github.com/22966422d5a62b100d96

Cheers,
Chris


On Tue, Jul 5, 2011 at 5:15 PM, Mike Bailey <[email protected]> wrote:
> I have a working SQL query I'm trying to convert to Arel. I'm trying to work
> out the syntax for a self referential join on a table.
>
> I can't work out how to address the table as 's1' and 's2' so my query makes
> sense.
>
> irb(main):018:0> s1.join(s2).on(s1[:serviceid].eq(s2[:serviceid]),
> s1[:id].gt(s2[:id])).where(s2[:serviceid].eq(nil)).to_sql
> => "SELECT FROM `service` INNER JOIN `service` ON `service`.`serviceid` =
> `service`.`serviceid` AND `service`.`id` > `service`.`id` WHERE
> `service`.`serviceid` IS NULL"
>
> Has anyone done this?
>
> - Mike
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rails-oceania?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.

Reply via email to