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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:56:53+00:00 2026-06-13T03:56:53+00:00

I found this article: Override a Mongoid model's setters and getters to help me,

  • 0

I found this article: Override a Mongoid model's setters and getters to help me, but behavior’s still not what I am looking for.

My model is like so:

class Event
  include Mongoid::Document
  include IceCube

  validates_presence_of :title
  field :title, type: String
  field :description, type: String
  field :schedule, type: Hash

  attr_protected :schedule_hash

  def schedule=(new_schedule)
    super(new_schedule.to_hash)
  end

  def schedule
    Schedule.from_hash(super())
  end
end

This works more or less as I’d expect, in that it serializes and deserializes the IceCube object, but what I noticed is that while I can do this:

s = Schedule.new(Time.now)
s.add_recurrence_rule Rule.daily
e = Event.new
e.schedule = s

it seems to serialize and deseralize as I would expect, and I can call things like

e.schedule.occurs_at?(Date.today + 1.day) 

and get the expected response. However, if I try:

e.schedule.add_recurrence_rule Rule.daily

instead of calling it on the local variable s before setting the property on event, I can look at the hash and see the rule is not persisted.

Is there something I’m missing on the right way of doing this sort of thing in Ruby or Mongoid?

I tried using write_attribute and read_attribute, but that was likewise to no avail.

  • 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-13T03:56:55+00:00Added an answer on June 13, 2026 at 3:56 am

    While not the most pretty, this strategy will work:

    
    
    class Event
      include Mongoid::Document
    
      validates_presence_of :title
      field :title, type: String
      field :description, type: String
      field :schedule
    
      before_save do
        write_attribute(:schedule, @schedule.to_yaml)
      end
    
      def schedule=(s)
        @schedule = s
      end
    
      def schedule
        @schedule ||= begin
          yaml = read_attribute(:schedule)
          yaml ? IceCube::Schedule.from_yaml(yaml) : IceCube::Schedule.new
        end
      end
    end
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this article but it does not contain support for run as admin..
I googled and found this article but it didn't work with phpBB. Would you
I found this article who explain how to use cache items expiration to make
I found this article about how to insert, update and delete using linq pad
I found this article on how to manipulate the rendering sequence of asp.net controls.:
I'm learning about WCF and found this article on a simple WCF example. In
I found this older article how-to-monitor-the-computers-cpu-memory-and-disk-usage-in-java and wated to ask, if there is something
I found this on net in google search and see article here: http://www.thatcssguy.com/limit-your-divs/ See
I recently found an article online that told me about this: RewriteRule ^mock-up/([^/]+)/([^/]+) /mock-up/index.php?page=$1&section=$2
I have done some looking and I found this: http://www.codeproject.com/Articles/14439/The-ScrollableListBox-Custom-Control-for-ASP-NET-2 but to me it

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.