ariejan (owner)

Revisions

gist: 25721 Download_button fork
public
Public Clone URL: git://gist.github.com/25721.git
Embed All Files: show embed
sti_update_attributes.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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