I realised, in the time it took my post to appear, that this is absolutely possible to model in Sequel, and is in fact quite straightforward; you simply pass a block to the association:

one_to_many :attachments, :key => :post_parent, :class => self do |ds|
      ds.where(:post_type => 'attachment')
    end

Hope this is useful to someone else who's in the same position as me! (And please do let me know if it isn't the most efficient/best way to do things.)

Best,
Rob


On 6 Sep 2013, at 12:25, Rob Miller wrote:

Hi guys,

I'm working with a legacy schema for a website. It has a posts table, and posts are hierarchical; so, each post has a post_parent column, which is 0
for top-level posts and contains the ID of a post for posts that are
children.

I have this association modelled fine with a self-association:

one_to_many :children, :key => :post_parent, :class => self

This gives me post.children, an array containing all of the children.
That's great.

However, there's an extra step. This schema implements a sort of Single
Table Inheritance, whereby these children also have types.

If their post_type is the same as the parent, they're regular posts and
should be included in the "children" association as above.

But posts that have post_type "attachment" should be in an "attachments"
association; and posts that have post_type "revisions" should be in a
"revisions" association.

So I basically want to extend this association code:

one_to_many :children, :key => :post_parent, :class => self
one_to_many :revisions, :key => :post_parent, :class => self
one_to_many :attachments, :key => :post_parent, :class => self

to have conditions on the second two associations that are the equivalent
of, in SQL, `JOIN posts p2 ON p1.ID = p2.ID AND p2.post_type =
"attachments"', and a condition on the first that is the equivalent of
`JOIN posts p2 ON p1.ID = p2.ID AND p1.post_type = p2.post_type`.

If I were to phrase what I'm trying to achieve as some sort of procedural
pseudo-code:

foreach ( children as child )
if child.post_type == parent.post_type
add to "children" association
elsif child.post_type == "attachments"
add to "attachments" association
elsif child.post_type == "revisions"
add to "revisions" association

Can this type of association be modelled in Sequel?

Many thanks,
Rob Miller

P.S. for those who guessed, yes, the legacy schema I'm talking about is WordPress — I'm in the early stages of writing an ORM wrapper for it so I
don't have to delve into PHP to work on our many WordPress sites:
https://github.com/robmiller/ruby-wpdb

--
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 http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.

--
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 http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to