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

  • Home
  • SEARCH
  • 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 8550419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:56:47+00:00 2026-06-11T13:56:47+00:00

Is there a DSL for creating an object in an AR relationship that would

  • 0

Is there a DSL for creating an object in an AR relationship that would be the opposite of :dependent => destroy (in other words create an object so that it always exists). Say, for example, I have the following:

class Item < ActiveRecord::Base
  #price
  has_one :price, :as => :pricable, :dependent => :destroy
  accepts_nested_attributes_for :price
  ....

class Price < ActiveRecord::Base
    belongs_to :pricable, :polymorphic => true
    attr_accessible :price, :price_comment

I’m thinking I’d like for a price to be created every time even if we don’t specify a price? Is the only (or best) option to do this as a callback or is there a way to do this via a DSL (similar to :denpendent => :destroy)?

  • 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-11T13:56:48+00:00Added an answer on June 11, 2026 at 1:56 pm

    No, as there is virtually no use-case for this. If your record cannot exist without an associated record, you should probably be preventing the record from being saved, not shoe-horning in some kind of pseudo-null object to take its place.

    The closest approximation would be a before_save callback:

    class Item < ActiveRecord::Base
      has_one :price, :as => :pricable, :dependent => :destroy
      accepts_nested_attributes_for :price
    
      before_save :create_default_price
    
      def create_default_price
        self.price ||= create_price
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I need to create my own small DSL that would use Python to
Is there a way to define a dsl, that would allow the following form?
There is a column that exists in 2 tables. In table 1, this column
I'm trying to create a DSL that can easily use a dom node. Using
I have an option parsing gem that provides a DSL for creating commands. In
There's a new dsl in capybara 1.0.0.rc1 that's very similar to steak. I'm trying
I would like to create a simple file format/DSL which would allow my users
I have a relatively simple DSL that I would like to handle more robustly
Anyone out there using YAML as a data DSL in .NET? I'd like to
There is a moment in my app, that I need to force to show

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.