Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# STI, this is the base model which is stored in the database
class Character < ActiveRecord::Base
belongs_to :location
def move_to(new_location)
update_attribute(:location_id, new_location.id)
# or:
# self.location = new_location
# save
end
end
class PlayerCharacter < Character
end
location_1 = Location.find(1)
location_2 = Location.find(2)
# Behold the following:
@player_character.location => location_1
@player_character.move_to(location_2) => true
@player_character.location => location_1 # Even after @player_character.reload
# This does work:
@player_character.location => location_1
@player_character.location = location_2
@player_character.save
@player_character.location => location_2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment