2016-09-14 75 views
0

我有一個名爲分配多態模型,通過owner_type屬於兩個任務項目,owner_id。導軌(4)通過動態命名範圍多態模型

任務&項目的範圍稱爲:即將到來:今天和:this_month。我正在嘗試使用一個名爲作用域的作用域來獲取我需要的所有數據......但它並不工作。

在這個例子中,「時間線」承擔責任:水災等於「今天」,「THIS_MONTH」或「即將到來的」

查詢將

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

這裏是我的作業範圍命名(Task.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.timeline」「與owner_class.this_month」,但我想動態設置在owner_type命名的範圍。

回答