[英]Updating a polymorphic association in rails
I took over a project several months ago that involved polymorphic associations that were infinitely deep.. and I'd like to get some help understanding how to operate on the models. 几个月前,我接手了一个项目,该项目涉及无限深的多态关联。.我想获得一些帮助,以了解如何在模型上进行操作。
This is a sample of how the models look like: https://gist.github.com/1701475 这是模型外观的示例: https : //gist.github.com/1701475
CallFlow 呼叫流
-- RouteByXxxx -RouteByXxxx
---- XxxxOption ---- Xxxx选项
There are actually 6 associations to CallFlow (RouteByXxxx) which all have the child(ren) XxxxOption that is a list of options. 实际上,有6个与CallFlow(RouteByXxxx)的关联,所有关联都具有作为子选项列表的子级XxxxOption。
schema.rb: https://gist.github.com/1701475 schema.rb: https ://gist.github.com/1701475
My biggest question: how would I go about updating the target_did in one of the IvrOption records when I only have the DNIS associated with a CallFlow? 我最大的问题:当我仅将DNIS与CallFlow相关联时,如何在IvrOption记录之一中更新target_did?
Realistically, an association looks like this: 实际上,一个关联如下所示:
CallFlow (routable_type=RouteByMessage) 呼叫流程(routable_type = RouteByMessage)
-RouteByMessage -RouteByMessage
--MessageOption (target_routable_type=RouteByIvr) --MessageOption(target_routable_type = RouteByIvr)
---RouteByIvr --- RouteByIvr
----IvrOption (target_routable_type=null, target_did=1112223333) ---- IvrOption(target_routable_type = null,target_did = 1112223333)
Something like this should work; 这样的事情应该起作用; just follow the association chain.
只需遵循关联链。 There's no checking for nils or anything here, so you'd want to add that, but the idea should hold.
这里没有检查nil或任何东西,因此您想添加它,但是这个想法应该成立。
call_flow = CallFlow.find_by_dnis(params[:dnis])
target_routable = call_flow.routable.options.first(:conditions => ["target_routable_type = ?", 'route_by_ivr']
ivr_option = target_routable.options.find(:conditions => (whatever))
ivr_option.update_attributes :target_did => 112233
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.