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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:38:47+00:00 2026-05-26T06:38:47+00:00

I have an Appointment model, whose available_dates can include multiple dates (say, available in

  • 0

I have an Appointment model, whose available_dates can include multiple dates (say, available in Jan 1, 5, 6 and 7).

My question is very basic: how should I store available dates for each event?

What I can think of is a table Avaiable_Dates with two columns: event_id and date. Each event would have multiple rows, one date per row. It seems to be cumbersome to query entire table to make sure we got all dates of an event. A Hash {event => {date1, date2, date3}} would be faster, but I don’t know how to implement it using ActiveRecord.

Thank you.

  • 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-26T06:38:48+00:00Added an answer on May 26, 2026 at 6:38 am

    It might not be a bad idea to just use the separate model for available times, but if you decide to go the hash route you can do so using the serialize keyword. You have to tell ActiveRecord to serialize the variable, and then it will do the serialization and deserialization automatically whenever you access the hash.

    Saving arrays, hashes, and other non-mappable objects in text columns

    Active Record can serialize any object in text columns using YAML. To do so, you must specify this with a call to the class method serialize. This makes it possible to store arrays, hashes, and other non-mappable objects without doing any additional work.

    class User < ActiveRecord::Base
      serialize :preferences
    end
    
    user = User.create(:preferences => { "background" => "black", "display" => large })
    User.find(user.id).preferences # => { "background" => "black", "display" => large }
    

    You can also specify a class option as the second parameter that’ll raise an exception if a serialized object is retrieved as a descendant of a class not in the hierarchy.

    class User < ActiveRecord::Base
      serialize :preferences, Hash
    end
    
    user = User.create(:preferences => %w( one two three ))
    User.find(user.id).preferences    # raises SerializationTypeMismatch
    

    When you specify a class option, the default value for that attribute will be a new instance of that class.

    class User < ActiveRecord::Base
      serialize :preferences, OpenStruct
    end
    
    user = User.new
    user.preferences.theme_color = "red"
    

    From rubyonrails.org

    From a design perspective, if you think you will ever add any more data to the available time object then you should make it its own model. Otherwise a serialized hash seems fine.

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

Sidebar

Related Questions

I have a Meeting model, with multiple Participants in different roles (say, Tutor and
I have a simple application with a customer and appointment models, system admin can
In a django application I have the following model: class Appointment(models.Model): #some other fields
I have a django model for an appointment in a calendar that I am
I have an Appointment model where each Appointment has a client_id . When an
I have a page where the user can select three different appointment days using
Lets say I have this in my controller @appointment.add_appointer Is there a shortcut in
I have an Application (an appointment manager) which lets the user create usercontrols in
We have the following data model: CalendarAppointment 1 <------> * AppointmentRole * <--------> 1
Ok, I have a generic TimeSlot model that deals with a start_at and an

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.