I have two models :
class Game
before_save :update_teacher
teacher
end
def update_teacher
teacher.update_attribute("something", true)
end
end
class Puzzle < Game
belongs_to :teacher
end
I have many types of games. When any game is complete, I’d like to update_teacher.
But as you can see, Game does not belong to anyone. It’s just where I keep all my global methods for all the games. I would never need to query Teacher.games. Instead, I only need to query Teacher.puzzles, or Teacher.riddles and so on.
It is because of this, that when I come to the before_save method, and I try to call teacher, it will fail because teacher is not associated with game.
So how can I keep my global Game class handling this method and still refer to its child’s association?
Also..
I just realized that this before_save might not actually ever be called because it’s not the Game model that’s updating ( or is it? ). If it isn’t..same question, how do I globalize this inherited method properly?
Alternatively..
I will admit that there might be an architectural flaw to how I’m going about my association. Would anyone recommend that I create two associations, or even just one association from Game directly with a game_type. Not sure what would be better or worse.
If every game has a teacher, the
belongs_to :teachershould be in theGameclass and not in the subclass.When you add a
before_savein theGameand save aPuzzleit will call thebefore_savefrom theGamebecausePuzzleis a game, butGamehas no knowlege of:teacher.Please update your question with a more detailed description of what you want to accomplish and not of the circumstances.
UPDATE
What you can do, is have a method that is called on the parent class and is overridden by the child classes