0

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)

Ngoral
  • 4,215
  • 2
  • 20
  • 37
  • My best guess is you're not persisting the provider change. In your controller the change can be seen because the provider has just been assigned. Inside the worker you re-instantiate Air and its associations, but the provider assignment is lost. – Alexandre Angelim Sep 30 '16 at 22:17
  • @AlexandreAngelim Please, see an update. I also save both provider and restream several times in concern function, that's called inside controller. But still adding a force saving before the end of controller function does not do the thing – Ngoral Oct 01 '16 at 12:15
  • Can your show me the code where you assign the new provider to the restream? – Alexandre Angelim Oct 01 '16 at 13:40
  • @AlexandreAngelim Here it is after the question and before UPD's. It looks pretty bad now, cause I tried to change it in so many ways, I sometimes forget what I really wanted to do =) – Ngoral Oct 01 '16 at 19:05
  • @AlexandreAngelim putting testing printing more and more, again and again, I've found out that before `old_provider.restream = nil` record in DB has good associations, and after it everything failes. Why can it be so? E.g. `new_provider.restream.to_json` returns `null`. That's in case it updates `restream` and set `restream.provider` to `nil`? How can I cansel this updating? – Ngoral Oct 01 '16 at 19:46
  • https://gist.github.com/angelim/3186cee39166a5e39a033444c99df3c8 – Alexandre Angelim Oct 01 '16 at 22:16
  • @Alexandreangelim //if for some reasons you haven't received a notification: I left a comment there :) – Ngoral Oct 02 '16 at 19:29
  • @AlexandreAngelim I've done this! adding `reload` before saving restream like this: `restream.reload.update_attributes(provider: new_provider)` done the thing! Just left to understand how does this works. – Ngoral Oct 03 '16 at 06:43

1 Answers1

0

The thing that helped me: adding reload to restream before updating parameters like this: restream.reload.update_attributes(provider: new_provider)

Ngoral
  • 4,215
  • 2
  • 20
  • 37