I got a bug in a bit of a complex part of a rails app and was curious when you need to use self to refer to the current object in a Rails model. I had (with the problematic lines beneath and commented out):
class Item < ActiveRecord::Base
....
def item_instore_enabled_changed list_is_instore_enabled # would be true or false
if item_is_instore_enabled
Rails.logger.info("this will be true with is_enabled_item: #{is_enabled_item}")
self.is_enabled=true if self.is_enabled_item
# is_enabled=true if is_enabled_menu_item - this didn't work
end
Rails.logger.info("BEFORE item save and is_enabled: #{is_enabled}")
self.save!
# save - didn't work
Rails.logger.info("AFTER item save and is_enabled: #{is_enabled}")
end
Basically, I had to specify self.is_enabled
rather than just is_enabled
to assign a value and also could just call save!
and had to call self.save!
.
What are the rules for referring to self within an object or was there some other bug causing a problem with the above code?