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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:35:50+00:00 2026-05-16T12:35:50+00:00

I have a Service model with title:string description:string date:datetime. I would like to implement

  • 0

I have a Service model with title:string description:string date:datetime. I would like to implement a system to create multiple services based on date patterns, e.g. when the user create a new service for Oct 20 he can choose to “repeat” it once a month for 5 months. The final result should be that 6 services are created, due to Oct 20, Nov 20, etc.
The way I implemented this is working, but really ugly. I have something like this in my view:

<input type="radio" name="each" value="none" checked="true"/>No<br/>
<input type="radio" name="each" value="week"/>Each week for <input type="text" name="weeks_number" /> weeks.<br/>
<input type="radio" name="each" value="month"/>Each month for <input type="text" name="months_number" /> months.<br/>

In my controller, I calculate the dates the various services will take place, than create N Service objects (where N is the number specified in params[:weeks_number] or params[:months_number]) and save each of them.

This doesn’t sound good, does it? First of all, I was thinking about moving all the logic for creating “multiple” services into the model. The next big thing is: how to clean up the view and the controller? I’d like my controller to be as simple as

@service = Service.new(params[:service])

In order to do this I’ll have to change something in the model (this should be called virtual attributes) in order to let the controller believe that something like

@service.repeat_method = "month"
@service.repeat = 6

actually makes sense and will cause the creation of 6 different rows in my db, when calling @service.save.

Here is where I’m stuck and in need of help. Does my reasoning make any sense? And how to implement this?

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-16T12:35:50+00:00Added an answer on May 16, 2026 at 12:35 pm

    I wouldn’t have the repeat_method and repeat attributes on a Service instance doing the magic you describe in the question. I’d have a create_repeats method something like this:

    # Create repeats of this Service
    # how_many:: the number of repeats to create
    # repeat_method:: whether to repeat 'weekly' or 'monthly'
    def create_repeats(how_many, repeat_frequency)
      # Set the first thing to be cloned as this instance
      rep = self
    
      # Create how_many copies increasing the date each time
      how_many.times do
        rep = rep.clone
        if repeat_frequency == 'weekly'
          rep.date += 1.week
        elsif repeat_frequency == 'monthly'
          rep.date += 1.month
        else
          raise "Unrecognized repeat frequency."
        end
        rep.save!
      end
    end
    

    Then your controller code will be something like the following:

    @service = Service.new(params[:service])
    if @service.save
      if !params[:weeks_number].blank?
        repeats = params[:weeks_number].to_i
        repeat_frequency = 'weekly'
      elsif !params[:months_number].blank?
        repeats = params[:months_number].to_i
        repeat_frequency = 'monthly'
      end
      @service.create_repeats(repeats, repeat_frequency)
    end
    

    I know this doesn’t defer all the logic to the model but some parameters really need specific logic to determine how they are translated from the view to the model layer.

    You may also want to check that params[:weeks_number] or params[:months_number] are a valid number and that the user has not specified both.

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

Sidebar

Ask A Question

Stats

  • Questions 520k
  • Answers 520k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Short answer... no. Applescript is a tool for controlling other… May 16, 2026 at 8:41 pm
  • Editorial Team
    Editorial Team added an answer Keep the changing of the password and any changes to… May 16, 2026 at 8:41 pm
  • Editorial Team
    Editorial Team added an answer Forget about timers, they are just too much work in… May 16, 2026 at 8:41 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Consider the following scenario: You have an account model You have an external service
I would like to expose a SyndicationFeedFormatter with wcf and basicHttpBinding. I continue to
I have created an OData/WCF service using Visual Studio 2010 on Windows XP SP3
I want to test a Grails controller which calls a service. I'd like to
I have found myself with at little problem, and I think a custom model
In a MVC project am trying to create a view which has the Title
I'm looking for possible solutions for the following scenario: I have a service that
I have a simple ADO.NET Entity Framework 4.0 model (edmx) which defines database tables
I have an existing Django application with a pretty typical model implementation that's backed
I have a Data Access Layer, a Service Layer, and a Presentation Layer. The

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.