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).
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?
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
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?
Thanx!
======================
Code that assigns new provider
to 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
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 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
)