Skip to content

Instantly share code, notes, and snippets.

@ariejan
Created November 17, 2008 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariejan/25721 to your computer and use it in GitHub Desktop.
Save ariejan/25721 to your computer and use it in GitHub Desktop.
# 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