I ended up refactoring this to remove CommentGroup. The controller
code for CommentGroup was too ugly.

Here's how my associations ended up:

class User < ActiveRecord::Base
  has_many :subscriptions
end
class Comment < ActiveRecord::Base
  belongs_to :author, :class_name => "User"
  belongs_to :commentable, :polymorphic => true
end
class Subscription < ActiveRecord::Base
  belongs_to :subscribable, :polymorphic => true
  belongs_to :user
end
class BlogPost < ActiveRecord::Base
  has_many :subscriptions, :as => :subscribable
  has_many :subscribers, :through => :subscriptions, :source => :user
  has_many :comments, :as => :commentable
end

Subscribable and Commentable are basically the same thing.

Then I added routes that appear as nested routes for any of the
commentable/subscribable types:

  map.comments(":commentable_type/:commentable_id/comments",
               :controller => :comments,
               :action => :create,
               :conditions => {:method => :post}
               )

  map.subscribers(":subscribable_type/:subscribable_id/subscribers",
                  :controller => :subscribers,
                  :action => :update,
                  :conditions => {:method => :post}
                  )
  map.subscribers_table(":subscribable_type/:subscribable_id/
subscribers/table",
                        :controller => :subscribers,
                        :action => :table,
                        :conditions => {:method => :get}
                        )

Then I have controllers and views for Comments and Subscribers that
operate on commentable or subscribable and don't care what type they
are.

To enable an additional model class as commentable (and subscribable)
I just add the three has_many lines (like BlogPost), and add the
comments partial to the appropriate view.

Next: how can I DRY up those three has_many lines? I want to put
something like 'acts_as_commentable' in the model and have it do those
three has_many lines.


On Oct 16, 10:31 am, Scott Johnson <[email protected]> wrote:
> On Oct 16, 8:43 am, Marnen Laibow-Koser <rails-mailing-l...@andreas-
>
> s.net> wrote:
> > Scott Johnson wrote:
> > > A nested set seems a little overkill.
>
> > It's not overkill at all if you're threading your comments.
>
> Good point, and I hadn't thought of that, but I don't need nesting.
>
> > > I realized I could make two polymorphic associations: one in Comments,
> > > and one in the join table for commentable_subscribers.
>
> > > I'm not sure if that's a better plan or not, though. I like that I can
> > > enforce foreign key constraints when I use the extra CommentGroup
> > > table.
>
> > Why not keep CommentGroup and have a polymorphic association that will
> > bind it to either BlogPost or BugReport?
>
> That sounds like the best plan. Thanks for the help.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: 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/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to