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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:16:12+00:00 2026-06-04T21:16:12+00:00

I have two models that I currently have as STI. class Event class BlockedTime

  • 0

I have two models that I currently have as STI.

class Event
class BlockedTime < Event
class Appointment < Event

The models are similar except that ‘Appointment’ has_one ‘client’ and has_one ‘service’, whereas ‘BlockedTime’ has neither of those associations.

Additionally, I want to be able to convert between the two types easily, and when I search, I’m usually searching on both types together, not one or the other. For those reasons I hesitate to make them separate tables.

For switching between the types, I currently do this in the same form, using the parent ‘Event’ type, and storing the ‘type’ as a hidden field, but this is definitely not working out well. I have to do all sorts of conversions in the controller to check validations, etc.

How would you recommend modeling this?

  • 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-04T21:16:13+00:00Added an answer on June 4, 2026 at 9:16 pm

    So, if I were having this problem I would probably model the database differently.

    Instead of a STI/Inheritance pattern, I would use a Composition pattern. So:

    class Appointment
      belongs_to :block_time, :class_name => "BlockTime"
      ...
    end
    
    class BlockTime
       has_one :appointment
       accepts_nested_attributes_for :appointment, :allow_destroy => :true, 
                                                   :reject_if => :all_blank
    
      ...
    end
    

    Controller

    class AppointmentsController < ApplicationController
      ...
    
      def new
        @block_time = BlockTime.new
        @block_time.appointment or @block_time.build_appointment
        ...
      end
    
      def edit
        @block_time = BlockTime.includes(:appointment).find(params[:id])
        @block_time.appointment or @block_time.build_appointment
      end
    
      ...
    end
    

    Form or at least part of the form

    <%= f.fields_for :appointment do |g| %>
      <div>
        <%= g.radio_button :_destroy, "1", :checked => !g.object.persisted? %>
        <%= g.label :_destroy_1, "Has no Appointment" %>
    
        <%= g.radio_button :_destroy, "0", :checked => g.object.persisted? %>
        <%= g.label :_destroy_0, "Has Appointment" %>
      </div>
    
      <p>
        Client: <%= g.text_field :client %>
      </p>
    <% end %>
    

    This part of the form uses persisted? to check to see if the Appointment object is a new record or has been persisted to the database. Then if it has been and you want to remove it then it will throw the _destroy flag for accepts_nested_attributes_for and delete the existing appointment association to make it a free BlockTime.

    Then you could include all the Appointment fields in this form too. You might want to cook up some javascript to disable/enable fields depending on the radio_button selection

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

Sidebar

Related Questions

I have two models that are associated with each other. Customer has_one :primary_contact And
I have two models, users and themes, that I'm currently joining in a HABTM
I have a form that calls on two separate models. My validation works correctly
If I have two models that are guaranteed to have a one-to-one correspondence, i.e.
Let's assume I have two gmdistibution models that i obtained using modeldata1=gmdistribution.fit(data1,1); modeldata2=gmdistribution.fit(data2,1); Now
I have two models -- User and Entry -- that are related through a
I have two models (questions and answers) that slightly follow the popular Railscasts :
I have two models User and Admin(with RailsAdmin) that use Devise. I sign in
I have two models: Users and Projects. The idea is that Users can follow
I have an application that uses Core Data. I have two data models in

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.