I am implementing this solution as an alternative to polymorphism.
Why can you not have a foreign key in a polymorphic association?
But I am wondering if there is an easy way to go:
Article.comments
instead of Article.commentable.comments
I am implementing this solution as an alternative to polymorphism.
Why can you not have a foreign key in a polymorphic association?
But I am wondering if there is an easy way to go:
Article.comments
instead of Article.commentable.comments
You should be able to use delegate
to pass method calls on to another object.
class Article < ActiveRecord::Base
has_one :commentable
delegate :comments, :to => :commentable
end
Edit:
I'm assuming you don't mean to use the constant Article in your example, because that wouldn't work either way. These methods are instance methods and need to be used as:
article = Article.first
article.commentable.comments
article.comments (Equivalent to above)