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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:15:44+00:00 2026-06-05T13:15:44+00:00

I have 2 models, User and Stash . A User has many stashes, and

  • 0

I have 2 models, User and Stash.

A User has many stashes, and a default_stash. A Stash has one owner (User).

Before a User is created I want it to build a Stash AND assign that Stash to self.default_stash. As per the instructions here Rails – Best-Practice: How to create dependent has_one relations I created the Stash relation, however I cannot ‘create’ the belongs_to relation at the same time.

User.rb

has_many :stashes, foreign_key: :owner_id
belongs_to :default_stash, class_name: 'Stash', foreign_key: :owner_id

before_create :build_default_stash
def build_default_stash
  self.default_stash = stashes.build
  true
end

Stash.rb

belongs_to :owner, class_name: 'User'

At this point the Stash can be found in user.stashes but user.default_stash remains nil, as stashes.build does not return an id in the before_create block.

What I need can be achieved by adding the following to User.rb

after_create :force_assign_default_stash
def force_assign_default_stash
  update_attribute(:default_stash, stashes.first)
end

But I’d much prefer to keep everything within the before_create block if possible, for validations etc.

  • 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-05T13:15:45+00:00Added an answer on June 5, 2026 at 1:15 pm

    I agree with you that what you describe should work, but if you’re building associated records in memory and expecting them to save together properly — with everything hooked up — then ActiveRecord is particularly finicky about how you define them.

    But you can make it work without changing a thing in your before_create.

    The usual problem is that you need to give AR hints about which relationships are inverses of each other. The has_many and belongs_to methods take an :inverse_of option. The problem in your case is that you have one side of a relationship (Stash#owner) that is actually the inverse of two on the other (User#stashes and User#default_stash), so what would you set as the inverse_of for Stash#owner?

    The solution is to add a has_one to Stash (call it something like owner_as_default), to balance things out. Then you can add inverse_of to all of the definitions, each identifying its inverse on the other side. The end result is this:

    class User < ActiveRecord::Base
      has_many   :stashes,       foreign_key: :owner_id, inverse_of: :owner
      belongs_to :default_stash, class_name: "Stash",    inverse_of: :owner_as_default
    
      ...
    end
    
    class Stash < ActiveRecord::Base
      belongs_to :owner,            class_name: "User", inverse_of: :stashes
      has_one    :owner_as_default, class_name: "User",
        foreign_key: :default_stash_id, inverse_of: :default_stash
    end
    

    (Also, you don’t need a foreign key on your belongs_to.)

    From there, your before_create should work as you’ve written it.

    Yes, it seems that this is a lot of redundant defining. Can’t ActiveRecord figure it all out from the foreign keys and whatnot? I always feel like I’m breaking some walls by making one side so aware of what it’s the inverse of. Maybe in some future version of Rails this will be ironed out.

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

Sidebar

Related Questions

I have three models: User Award Trophy The associations are: User has many awards
I have two models: User and HairColor. A user has only one hair color,
I have several Models: Workspace User Asset WorkspaceUser WorkspaceAsset Workspace has many users and
I have three models: User, Task and Assignation. User has many tasks through assignation.
I have two models: User MentoringRelationship MentoringRelationship is a join model that has a
I have 3 models: user, seller and cars i want the user to be
I have two models: User has_many :prices Price belongs_to :users I want to show
Basically I have two models: User and Godfather. The godfather table has three columns:
I have two models: UserProfile (extended from user) and Cv . I created another
I can't get how to do one thing. I have models User , Teacher

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.