问题描述
我有一个问题,我听不懂...
在我的项目中,我有几种模型。我有,每个重播都有一个提供者(这是一个多态关联)。
I have a problem, that I can't understand...In my project I has several models. I have channel that has many restreams and each restream has one provider (it's a polymorphic assosiation).
所以在代码中,我用另一个替换了一个 restream.provider ,最后, restream.provider 的值为 nil 。我尝试放置大量测试输出,所以现在有了这样的结果:在其中一个控制器中,我有一个代码:
So in code I replace one restream.provider with another, and in the end restream.provider has a value of nil. I tried to put a lot of testing output, so now I have such a result: in one of the controllers I have a code:
puts channel.restreams.last.provider.to_json byebug Delayed::Job.enqueue(StartRestreamJob.new(air.id)) if air.server.online?
和 puts 输出的正是我期望的结果:提供者对象,转换为json。但是在 start_restream_job 中,我也有代码:
and puts outputs exactly what I expect: provider object, converted to json. But in start_restream_job I also has code:
air = Air.find(air_id) channel = air.channel puts channel.restreams.last.provider.to_json byebug
,它只为我提供 null 字符串。结果,保存在数据库中的 restream 对象与 provider 没有关联。
可能是什么问题?
and it provides me only with null string. As a result, restream object, saved in database, has no association with provider.What can be the problem with it?
感谢!
====== ===============
======================
将新的提供者分配给的代码 restream :
new_provider = ProviderType1.new(restream.provider.params_hash) #params hash provides some variables that I need to save from old provider old_provider = restream.provider new_provider.restream = restream res = new_provider.initialize_event #here some requests for external services are being made, after what some attributes of provider are being updated and after that provider is being saved by calling self.save! if res == ProviderType1.SUCCESS_CODE old_provider.restream = nil old_provider.save! old_provider.destroy new_provider.restream = restream restream.provider = new_provider restream.save! new_provider.save! else #smth that does not matter now end
========================
========================
UPD
即使增加了对重播和提供者的强制保存也没有任何意义:
Even adding force saving of both restream and provider do not make any sence:
channel.restreams.each do |r| r.provider.save! r.save end
我还有一个新的 provider 对象保存在数据库中,但与 stream 对象没有关系,反之亦然。
I still have a new provider object saved in DB, but it has no connection with restream object and vice versa.
============
=============
UPD 2
在令人敬畏的代码段中添加打印功能为我打开了新的东西:
Adding printing to prevoious piece of code opened a new things for me:
channel.restreams.each do |r| r.provider.save! r.save! puts ProviderType1.find(r.provider_id).restream.to_json
end
Puts打印出 null 。那么这是否意味着 r.provider.save!什么都不做?如果是,那为什么呢?为何保持沉默?
(这在代码中的某个位置运行,其中放置channel.restreams.last.provider.to_json 会打印出一个对象,而不是 null )
Puts prints out null. So does this mean r.provider.save! does nothing? And if yes, then why so? And why it's keeping silent?(This runs in a place in the code, where puts channel.restreams.last.provider.to_json prints out an object, not null)
推荐答案
帮助我的事情:添加重新加载到 restream ,然后更新像这样的参数: restream.reload.update_attributes(provider:new_provider)
The thing that helped me: adding reload to restream before updating parameters like this: restream.reload.update_attributes(provider: new_provider)
这篇关于Rails更新参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!