I am trying to update another associated table before saving but i am getting into errors i am not familiar with…
so i have in my model
post.rb
before_save :update_total_items
def update_total_items
b = Tags.find(:tag_id => self.tag_id)
b.total_items = b.total_items+1
b.save
end
but i get an error
Unknown key tag_id
so how can i access the property i am adding before i save it in order to use it and update an existing associated on another table?
Usually the
idof a model is called just that. Generally what you want is:Be aware this might throw an ActiveRecord::RecordNotFound exception if no record could be found. A safer approach is:
Avoid loading and saving to increment a value. You may end up with an ugly race condition where you lose counts.
As a note, it’s highly irregular that your model is called
Tags. It should be calledTag.