Tutorial :activerecord association preventing record from saving



Question:

I have a straightforward relation between two models: Asset and Update.

both models (minus unrelated methods) here:

http://pastie.org/1062627

I ran into a problem where an Asset record will no longer update. For example a simple test:

a = Asset.first ; puts a.description; a.description = "new" ; a.save ; puts a.errors ; puts Asset.first.description   

will generate:

"old"   {}    "old"  

a.save returns true, a.errors is empty; but the change is not saved.

I went through every item in my Asset model to try to figure out what was causing it, and found that if I removed the 'belongs_to :update' relation, then everything worked fine.

But how a relation to another table, with no validations involved, could prevent the record from saving, is a mystery. (Plus if it was a validation problem then @asset.errors would not be empty on the save attempt.)

To make matters stranger, I decided to rename my Update model to Report model. With that, it worked. So maybe Update is a reserved name for a model (seems unlikely). But I don't want to rename that model unless I have to (it's too many places already), so I'm thinking there must be something else here that's wrong or that I'm missing.

Any help is appreciated. Thanks.


Solution:1

Calling the association 'update' is a problem. To load the asset's associated update, you would have to do this:

@asset.update  

But update is an instance method that is already defined by ActiveRecord:

http://apidock.com/rails/ActiveRecord/Base/update


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »