Today, I had a bug crop up in our Ruby-on-Rails application. Somehow, the awsaccount_id field of our Device model is getting "marked for change" whenever we access the awsvpc association through the Device model.
To debug the problem, I added several log entries and several extra lines of code (as seen below) to diagnose how the device model instance is getting marked for change.
You may notice that the "dev.changes" method returns a pending change between logging "changes12" and "changes2". The only code that occurs between those log entries is accessing the association with dev.awsvpc.id
Under what circumstances can this ruby code:
Rails.logger.info "Update_Device changes0: #{dev.changes}"
sample_vpc = dev.awsvpc_id
Rails.logger.info "Update_Device changes1: #{dev.changes}"
sample_vpc = Awsvpc.find(dev.awsvpc_id)
Rails.logger.info "Update_Device changes12: #{dev.changes}"
sample_vpc = dev.awsvpc.id
Rails.logger.info "Update_Device changes2: #{dev.changes}"
Cause log entries that look like this:
Update_Device changes0: {}
Update_Device changes1: {}
[1m[36mAwsvpc Load (0.2ms)[0m [1m[34mSELECT "awsvpcs".* FROM "awsvpcs" WHERE "awsvpcs"."id" = $1 LIMIT $2[0m [["id", 644], ["LIMIT", 1]]
↳ app/lib/api/api_get_asset_result_builder.rb:53:in `populate_awssubnet'
Update_Device changes12: {}
[1m[36mCACHE Awsvpc Load (0.0ms)[0m [1m[34mSELECT "awsvpcs".* FROM "awsvpcs" WHERE "awsvpcs"."id" = $1 LIMIT $2[0m [["id", 644], ["LIMIT", 1]]
↳ app/lib/api/api_get_asset_result_builder.rb:55:in `populate_awssubnet'
Update_Device changes2: {"awsaccount_id"=>[66, 644]}
For anyone else who has this problem, I found the problem. In the models, we had: Device.rb
belongs_to :awsaccount, inverse_of: :devices, optional: true belongs_to :awsvpc, inverse_of: :devices, optional: true
Awsvpc.rb
Awsaccount.rb
As you can see, the Awsvpc model should have used inverse_of :awsvpc Once changed, everything works as expected.