[英]How do I call a before_save method from a parent model class?
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. 但是如您所见,Game不属于任何人。 It's just where I keep all my global methods for all the games.
在这里,我保留了所有游戏的所有全局方法。 I would never need to query
Teacher.games
. 我永远不需要查询
Teacher.games
。 Instead, I only need to query Teacher.puzzles
, or Teacher.riddles
and so on. 相反,我只需要查询
Teacher.puzzles
或Teacher.riddles
等。
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
. 因此,当我使用
before_save
方法并尝试调用teacher
,它将失败,因为teacher
与game
无关。
So how can I keep my global Game class handling this method and still refer to its child's association? 那么,如何使我的全局Game类处理这种方法,并仍然引用其子级的关联?
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? ). 我只是意识到实际上可能永远不会调用此before_save,因为不是Game模型在更新(或者是?)。 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
. 有人会建议我创建两个关联,或者甚至直接从
Game
使用game_type
创建一个关联。 Not sure what would be better or worse. 不知道会好还是坏。
If every game has a teacher, the belongs_to :teacher
should be in the Game
class and not in the subclass. 如果每个游戏都有一位老师,那么
belongs_to :teacher
应该在Game
类中,而不在子类中。
When you add a before_save
in the Game
and save a Puzzle
it will call the before_save
from the Game
because Puzzle
is a game, but Game
has no knowlege of :teacher
. 当您在
Game
添加一个before_save
并保存一个Puzzle
它会从Game
调用before_save
,因为Puzzle
是一个游戏,但是Game
没有:teacher
。
Please update your question with a more detailed description of what you want to accomplish and not of the circumstances. 请更新您的问题,并提供对您要完成的工作而不是具体情况的更详细描述。
UPDATE UPDATE
What you can do, is have a method that is called on the parent class and is overridden by the child classes 您可以做的是有一个在父类上调用并被子类覆盖的方法
class A
before_save :do_x
def do_x
raise "Please implement this!"
end
end
class B < A
def do_x
# do what B has to do
end
end
Sounds like Game
is a good candidate for a ActiveSupport::Concern
based Module
that's included in your game classes: 听起来,
Game
是您的游戏类中包含的ActiveSupport::Concern
Module
的不错的选择:
# app/models/concerns/game.rb
require 'active_support/concern'
module Game
extend ActiveSupport::Concern
included do
belongs_to :teacher
before_save :update_teacher
end
module InstanceMethods
def update_teacher
teacher.update_attribute("something", true)
end
end
end
# app/models/puzzle.rb
class Puzzle
include Game
end
This way belong_to
and before_save
are sent to Puzzle
when Game
is included. 这样,包括
Game
时, belong_to
和before_save
被发送到Puzzle
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.