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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:30:15+00:00 2026-05-13T06:30:15+00:00

I have an Event model with a finish_time field and a form checkbox called

  • 0

I have an Event model with a finish_time field and a form checkbox called whenever. When whenever is checked I want to set finish_time to nil regardless of its value in parameters, when whenever is not checked I want to make sure that finish_time is a valid date before I save it in the database.


Currently I am achieving this using a whenever attr_accessor on Event:

params[:event][:finish_time] = nil if whenever = params[:event].delete(:whenever)
@event = Event.new(params[:event])
@event.whenever = whenever

And using conditional validations to check for finish_time if whenever is false

validates_presence_of :finish_time, :unless => @whenever

I am not happy with the duplication this creates. The only way that finish_time can be nil is if whenever is true, and if you change one you often have to change the other. If the Event is updated with a new finish_time then whenever will have to be changed to false as well.

Ideally I would like to move the validation to my controller. That way when doing direct model access whenever can be easily indicated by setting finish_time to null, but users posting to /events will get yelled at if they haven’t indicated a choice. However I can’t figure out a way to conditionally add or remove validations to Event from the controller or even if this is the best approach.

What is the best way to remove this duplication?

  • 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-13T06:30:16+00:00Added an answer on May 13, 2026 at 6:30 am

    Instead of trying to solve problem via validation, you might try to address it in “a Ruby way”.

    In addition to validation, you might want to hook into mutators of finish_time and whenever methods.

    class Event < ActiveRecord::Base
      # Validate presence
      validates_presence_of :finish_time, :unless => @whenever
    
      def whenever=(yes)
        write_attribute(:whenever, yes)
        write_attribute(:finish_time, nil) if yes?
      end
      def finish_time=(time)
        write_attribute(:whenever, false)
        write_attribute(:finish_time, time)
      end
    end
    

    Then you can just try (however, I’m not sure whether it works):

    @event = Event.new(params[:event])
    

    Edit: However, this will make the resultant fields depend on the order assignemnts happen, so the results for the same parameter set may differ if the order of assignments changes. That’s unlikely what you want. So you must tune your opertators the way you want.

    And you also should understand that you won’t be able to make your code DRY unless you make your requirements DRY.

    Edit #2: I think you should consider dropping the requirement for finish_time to be nil if whenever is set to true. You can then modify accessors in the same way as described above.

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

Sidebar

Related Questions

In .NET, Windows Forms have an event that fires before the Form is loaded
I have a sharepoint event handler which I want to activate for a single
I have Event model which contains following fields string name date start_at date end_at
I have an edit view for an Event model with name , start_time ,
so I'm working on a django application where I have a model Event. Each
Working on a Calendar app, and would like each Event model instance to have
I have some event handler on a boundary class that manages a persistence mechanism
I have a simple event handler with a ItemAdding event that changes a column
I have 2 tables event + event_artist event eventId | eventName ------------------- 1 ,
I've got a list of links which have a click event attached to them,

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.