简体   繁体   中英

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

CallFlow
-- RouteByXxxx
---- XxxxOption

There are actually 6 associations to CallFlow (RouteByXxxx) which all have the child(ren) XxxxOption that is a list of options.

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?

Realistically, an association looks like this:

CallFlow (routable_type=RouteByMessage)
-RouteByMessage
--MessageOption (target_routable_type=RouteByIvr)
---RouteByIvr
----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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM