On Wednesday, December 28, 2016 at 8:20:10 AM UTC-8, Pedro VinÃcius wrote:
>
> I'm using Sequel in a personal project, but I need execute a self join.
> My application is a publications manager, and one of the business rule, is
> that a publication can belong to another publication based on its text
> similarity level.
> In pure SQL, I wrote the following query that worked well:
>
> SELECT publication.id, publicaton.body AS body, similar_publication.id AS
> similar_publication_id, similar_publication.body AS similar_body
> FROM publications
> LEFT JOIN publications similar_publications ON publications.publication_id
> = similar_publication.id;
>
>
> How can I "translate" this query to Sequel query DSL? I want call my model
> like so:
>
> Publication.including_similar_publications.where('publications.id = ?')
>
> I'm in doubt because I need an alias on the left join, and I don't know
> how to accomplish that. Can anybody help me?
>
You just need to alias the table during the join:
class Publication < Sequel::Model
dataset_module do
def including_similar_publications
select{[publications[:id], publications[:body],
similar_publication[:id].as(:similar_publication_id),
similar_publication[:body].as(:similar_body)]}.
left_join(Sequel[:publications].as(:similar_publication),
:id=>:publication_id)
end
end
end
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.