简体   繁体   English

如何从父模型类调用before_save方法?

[英]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.puzzlesTeacher.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 ,它将失败,因为teachergame无关。

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_tobefore_save被发送到Puzzle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在before_save方法中使用values_at? - How do I use values_at in a before_save method? Rails:如何将来自模型中的 before_save 的错误添加到控制器中? - Rails: How do I add into the controller an error that comes from a before_save in the model? 如果Rails中的ruby中的表单字段为空,如何停止运行before_save方法? - How do I stop a before_save method from running if a forms field is blank in ruby on rails? Rails 5 - 如何在before_save回调条件上否定方法调用 - Rails 5 - how to negate a method call on a before_save callback conditional 如何在插件的before_save回调中包含实例方法? - How do I include an instance method inside a before_save callback in a plugin? 如何在Rails 3的控制器中获取before_save值? - How do i get the before_save value in the controller in Rails 3? 如何在 before_save 钩子中将参数传递给模块? - How do I pass arguments into a module in a before_save hook? 通过子级before_save回调使父模型保存无效 - Invalidating parent model save through child before_save callback 为什么在保存之前使用if on时,为什么会得到false:FalseClass的未定义方法“ before_save”? - Why do I get undefined method `before_save' for false:FalseClass when using if on before save? before_save 方法 #1 如果方法 #2 == 0? - before_save method #1 if method #2 == 0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM