On Oct 28, 6:38 am, Scott LaBounty <[email protected]> wrote:
> Someone asked me on my blog about doing trees in Sequel. I tried the
> following:
>
> <<
> DB = Sequel.sqlite # Create an in-memory database
>
> # Create a new Post table with columns of
> # id, name, and popupost2tion.
> DB.create_table :posts do
>     primary_key :id
>     column :post, :text, :unique=>true
> end
>
> # Create a comments table that links to the
> # post with a foreign_key
> DB.create_table :comments do
>     primary_key :id
>     column :ct, :text
>     column :name, :text
>     foreign_key :post_id, :posts
>     foreign_key :comment_id, :comments
> end
>
> # Create a Post model stating that we'll have comments for each post.
> class Post < Sequel::Model;
>     one_to_many :comments
> end
>
> # Create a Comment model stating that a comment will belong to one post.
> class Comment < Sequel::Model
>     many_to_one :post
>     many_to_one :comment
>     one_to_many :comments
> end
>
>
>
> and everything seems to work fine. I have posts which have comments. Each
> comment belongs to a post but can also have a parent post.
>
> Is this the best way to do things? Is there anything I'm missing?

I'd name the associations parent_comment and child comments, or
something else.  But you've got the design correct.  Note that it's a
bit of work to get all descendant comments for a particular comment
unless your database supports recursive common table expressions.  For
database's that don't, you often see a recommendation to use a nested
set instead of a tree.

Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-talk" 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/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to