Jeremy, Thanks for the input. A couple of questions though:
I'm not sure what to rename when you say the associations. Do you mean in the table or in the model or both? Also, I'm not sure what a "nested set" is in the database world. I'll have to look that up. Thanks again. Scott On Wed, Oct 28, 2009 at 7:09 AM, Jeremy Evans <[email protected]>wrote: > > 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 > > > -- Scott http://steamcode.blogspot.com/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
