i have a question about associations in rails. The situation is the following:
Models:
class Character < ActiveRecord::Base
has_one :character_stats
end
class CharacterStats < ActiveRecord::Base
belongs_to :character
end
Now i need to create stats when a new character is created.
What i doo is this at the moment, i feel like this is a workaround with rails. Is there a more "raily" way to do this?
after_save :character_init
def character_init
create_stats
end
def create_stats
stats = CharacterStats.new
stats.character_id = self.id // this bothers me!
stats.save
end
But i feel there should be something like this:
stats.character << self
Thank You in advance :)
EDIT:
here is how my model look in real life:
def create_stats
race_stats = Race.find(race_id).race_stats
class_stats = RaceClass.find(race_class_id).class_stats
stats = CharacterStats.new
stats.character_id = self.id
stats.health = race_stats.health + class_stats.health
stats.mana = race_stats.mana + class_stats.mana
stats.intellect = race_stats.intellect + class_stats.intellect
stats.armor = race_stats.armor + class_stats.armor
stats.magic_resist = race_stats.magic_resist + class_stats.magic_resist
stats.attack = race_stats.attack + class_stats.attack
stats.defence = race_stats.defence + class_stats.defence
stats.save
self.character_stats_id = stats.id
end