在多态模型上传递动态命名作用域



我有一个称为分配的多态模型,它属于任务项目通过owner_type, owner_id.

任务,Item的作用域分别是:upcoming,:today和:this_month。我试图在任务上使用一个命名范围来获得我需要的所有数据……但它不太管用。

在这个例子中,"timeline"可能等于"today", "this_month"或"upcoming"

查询应该是

Assignment.by_timeline("task", "this_month")

这是我在分配(任务)上的命名范围。This_month独立运行):

     scope :by_timeline, lambda { |owner_type, timeline|
        owner = owner_type.to_sym
        owner_class = owner_type.camelize.constantize
        set_scope = timeline.to_sym
        scoped_owner = owner_class.timeline
        joins(owner).merge(scoped_owner)
  }

当我替换"owner_class。时间线"与owner_class。this_month",但我想动态地设置owner_type上的命名范围

最后我这样做了:

scoped_owner = owner_class.send("#{timeline}")

我使用了send

一切都归功于这个问题

最新更新