Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8172559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:49:23+00:00 2026-06-06T21:49:23+00:00

I have two models : class Game before_save :update_teacher teacher end def update_teacher teacher.update_attribute(something,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:49:25+00:00Added an answer on June 6, 2026 at 9:49 pm

    If every game has a teacher, the belongs_to :teacher should be in the Game class and not in the subclass.

    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.

    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

    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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
i have two models: class Category has many :jobs end class Job belongs_to :category
I have two models class Subscription < ActiveRecord::Base belongs_to :client end class Client <
I have two models as follows: class Game << ActiveRecord::Base has_many :bells end class
I have two models : class Event < ActiveRecord::Base belongs_to :destination end class Destination
I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =
I have two models: class Subscription belongs_to :subscriber, :class_name => User belongs_to :subscribable, :polymorphic
I have two models like this: class ClassA(models.Model): ida = models.AutoField(primary_key=True) classb = models.ForeignKey(ClassB)
I have two models like this: class OptionsAndFeatures(models.Model): options = models.TextField(blank=True, null=True) entertainment =
I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.