Ruby on rails 如何在装饰器中定义关联?

Ruby on rails 如何在装饰器中定义关联?,ruby-on-rails,associations,decorator,Ruby On Rails,Associations,Decorator,我不想用复杂的关联来污染我的模型(在AR模型中只有必要的验证和简单的关联) 有没有一种方法可以创建一个decorator类,这样就可以在其上声明关联(例如预加载关联)?还不完全清楚您想要做什么。“在上面声明关联”(decorator)很可能是一个很好的例子 为什么不这样做呢: class PostsDecorator < SimpleDelegator def visible_comments Comment.where(post: component, deleted_at

我不想用复杂的关联来污染我的模型(在AR模型中只有必要的验证和简单的关联)


有没有一种方法可以创建一个decorator类,这样就可以在其上声明关联(例如预加载关联)?

还不完全清楚您想要做什么。“在上面声明关联”(decorator)很可能是一个很好的例子

为什么不这样做呢:

class PostsDecorator < SimpleDelegator

  def visible_comments
    Comment.where(post: component, deleted_at: nil)
  end

private

  def component
    @component ||= self.__getobj__
  end

end

至于“预加载”的想法,我不知道该怎么做。

有没有办法做到这一点,是吗?@jvillian add decorator,这样就可以在其中定义关联了。我能想到的唯一方法是让你的decorator成为
ActiveRecord::Base
-关联是在AR中按类定义的。我认为这是设计模式的过度应用。我更新了这个问题,并试图进一步说明这个问题清楚的现在我想要什么更清楚了吗?对我来说有点像剃牦牛毛。
PostsDecorator.new(posts).preload(:visible_comments)
class PostsDecorator < SimpleDelegator

  def visible_comments
    Comment.where(post: component, deleted_at: nil)
  end

private

  def component
    @component ||= self.__getobj__
  end

end
PostsDecorator.new(posts).visible_comments