I am seeing a very strange issue with a simple controller method. Either I am missing something fundamental or I am encountering a bug. My bet is on the former.
I have a Thing model with a ThingController.
A Thing has two variables, name and display, both strings.
ThingController (code below) has a method toggle_display, that toggles the contents of display between "on" and "off".
The problem is that when I call this action, Rails finds the correct Thing, but @thing.display is nil. When I check the database, the value in the 'display' column is correct.
The strange part is that when I uncomment the third line in the code below (i.e. access @thing.name before accessing @thing.display) then @thing.display is fine - it isn't nil and it has the value I would expect. It's as if @thing.display only gets initialized correctly after I access @thing.name.
Any idea why I would see this very strange behavior?
def toggle_display
@thing = Thing.find(params[:id])
# @thing.name
if @thing.display
@thing.toggle_display_in_model
@thing.save
end
redirect_to things_url
end