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 6147915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:11:12+00:00 2026-05-23T19:11:12+00:00

I have a model, let’s say Cat , and I want to create a

  • 0

I have a model, let’s say Cat, and I want to create a new model, Owner. A Cat has_one :owner, but when I created the Cat model, the Owner model didn’t yet exist.

Without resorting to backfilling the new Owner model for each Cat, I want to have an easy way so if I call @cat.owner.something, and @cat.owner doesn’t exist yet, it will call @cat.create_owner on the fly and return it.

I’ve seen this done a few different ways, but I am wondering what is the most Rails-esque way of tackling this, since I need to do this quite often.

  • 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-05-23T19:11:13+00:00Added an answer on May 23, 2026 at 7:11 pm

    I haven’t seen this done before but decided to give it a shot anyways.

    I first aliased the association method owner in the Cat model to keep a backup of the original method. I overrode the owner method to call the build_owner method (returns a new Owner object through the association) if the original method returns nil. Otherwise, return the original_owner_method object.

    class Cat < ActiveRecord::Base
      has_one :owner
    
      alias :original_owner_method :owner
    
      def owner
        if original_owner_method.nil? 
          build_owner
        else
          original_owner_method
        end
      end
    

    So now if you call:
    cat = Cat.first

    Assuming it doesn’t have an owner, it will build a new Owner object when you call:
    cat.owner.name

    It will return nil, but still build the owner object on the cat.owner part of the chain without calling method_missing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a model class called Project, but instead of this: class
I know this is probably not possible but let's say I have a model
Let's say I have Book model and an Author model. I want to list
Let's say I have a Model, for example User, and I want to merge
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
Let's say I have this model named Product with a field named brand .
Let's say I have the following model: class Contest: title = models.CharField( max_length =
Let's say i have a Domain Model that im trying to make compatible with
I have a hibernate model with two classes, let's say A and B. B
For instance, let's say I have a User model. Users have things like logins,

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.