Arel does let you alias a table.
I might be improperly mixing the rails and pure Arel syntaxes, but
hopefully this will help:
Comment.scope :owned_by, lambda do |user|
p = Arel::Table.new(:post).alias("p")
joins(p).where(p[:user_id].eq(user.id))
end
Cheers,
Chris
> --
> 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 rails-...@googlegroups.com.
> To unsubscribe from this group, send email to
> rails-oceani...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rails-oceania?hl=en.
>
Thanks. Note taken :)
>> Comment.scope :owned_by, lambda do |user|
>> p = Arel::Table.new(:post).alias("p")
>> joins(p).where(p[:user_id].eq(user.id))
>> end
Yeah. I know this syntax, but it requires one additional line and then using that alias object in the 'where' clause.
What I ideally would like to write is something a bit closer to SQL, so that I can just say "this join is alias as `p`" and then use the p.
I realise what you suggested does exactly this, but I wonder if there is a more one-liner-ish way? Like so:
joins(:post, "p").where("p.user_id = ?", user.id)
it just reads easier than creating an alias as an object.
Nope, this sadly isn't possible. You're going to have to use straight-ARel for this or devise your own methods for it.