[英]Rails 4 Passing in dynamic named scopes on polymorphic models
I have a polymorphic model called Assignment that belongs to both Task and Item through owner_type, owner_id. 我有一个名为Assignment的多态模型,该模型通过owner_type,owner_id属于Task和Item 。
Task & Item have scopes called :upcoming and :today and :this_month. Task&Item的作用域称为:upcoming和:today和:this_month。 I am trying to use ONE named scope on assignments to get all the data I need...but it's not quite working.
我正在尝试在分配上使用一个命名范围来获取我需要的所有数据...但是效果不佳。
In this example, "timeline" coule equal "today", "this_month" or "upcoming" 在此示例中,“时间轴”的含义等于“今天”,“本月”或“即将来临”
The query would be 该查询将是
Assignment.by_timeline("task", "this_month")
here is my named scope on Assignment (Task.this_month works on it's own): 这是我在工作分配上的命名范围(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)
}
It works when I replace "owner_class.timeline " "with owner_class.this_month", but I want to set the named scope on the owner_type dynamically. 当我用owner_class.this_month替换“ owner_class.timeline”“,但我想动态设置owner_type的命名范围时,它可以工作。
I ended up doing this: 我最终这样做:
scoped_owner = owner_class.send("#{timeline}")
I used the send 我用了发送
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.