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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:19:20+00:00 2026-05-23T11:19:20+00:00

I have a nested form set up that will allow me to create up

  • 0

I have a nested form set up that will allow me to create up to 7 “Schedule” instances at one time. Each instance will allow a user to assign a schedule_start_time(datetime), schedule_end_time(datetime), and notes(String) field value. When I submit the form with a few of these completed, the parameters array looks exactly as I would expect:

{“utf8″=>”✓”,
“authenticity_token”=>”HEoylzovRgr7BCZH47iNRPfizDHeVFMLTEmIiNeudcw=”,
“workout”=>{“id”=>”2”,
“schedules_attributes”=>{

“0”=>{“scheduled_start_time”=>”06/01/2011”,
“scheduled_end_time”=>”06/02/2011”,
“notes”=>”Notes 1”},

“1”=>{“scheduled_start_time”=>””,
“scheduled_end_time”=>””,
“notes”=>””},

“2”=>{“scheduled_start_time”=>”06/03/2011”,
“scheduled_end_time”=>”06/04/2011”,
“notes”=>”Notes 2”},

“3”=>{“scheduled_start_time”=>””,
“scheduled_end_time”=>””,
“notes”=>””},

“4”=>{“scheduled_start_time”=>”06/16/2011”,
“scheduled_end_time”=>”06/30/2011”,
“notes”=>”Notes 3”},

“5”=>{“scheduled_start_time”=>””,
“scheduled_end_time”=>””,
“notes”=>””},

“6”=>{“scheduled_start_time”=>””,
“scheduled_end_time”=>””,
“notes”=>””}}}, “commit”=>”Submit”}

In the controller, I filter those “schedules” with a blank start_date. My params list then looks like this:

{“utf8″=>”✓”,
“authenticity_token”=>”HEoylzovRgr7BCZH47iNRPfizDHeVFMLTEmIiNeudcw=”,
“workout”=>{“id”=>”2”,
“schedules_attributes”=>{

“0”=>{“scheduled_start_time”=>”06/01/2011”,
“scheduled_end_time”=>”06/02/2011”,
“notes”=>”Notes 1”},

“2”=>{“scheduled_start_time”=>”06/03/2011”,
“scheduled_end_time”=>”06/04/2011”,
“notes”=>”Notes 2”},

“4”=>{“scheduled_start_time”=>”06/16/2011”,
“scheduled_end_time”=>”06/30/2011”,
“notes”=>”Notes 3”}}},

“commit”=>”Submit”,
“action”=>”create”,
“controller”=>”schedules”}

The SQL that gets generaated is not what I would expect however:

(0.1ms) BEGIN WARNING: Can’t
mass-assign protected attributes: id

SQL (0.2ms) INSERT INTO schedules
(created_at, notes,
scheduled_end_time,
scheduled_start_time, updated_at,
workout_id) VALUES (‘2011-06-29
03:23:45’, ‘Notes 1’, ‘2011-02-06
00:00:00’, ‘2011-01-06 00:00:00’,
‘2011-06-29 03:23:45’, 2)

SQL
(0.1ms) INSERT INTO schedules
(created_at, notes,
scheduled_end_time,
scheduled_start_time, updated_at,
workout_id) VALUES (‘2011-06-29
03:23:45’, ‘Notes 2’, ‘2011-04-06
00:00:00’, ‘2011-03-06 00:00:00’,
‘2011-06-29 03:23:45’, 2)

SQL
(0.2ms) INSERT INTO schedules
(created_at, notes,
scheduled_end_time,
scheduled_start_time, updated_at,
workout_id) VALUES (‘2011-06-29
03:23:45’, ‘Notes 3’, NULL, NULL,
‘2011-06-29 03:23:45’, 2) (0.5ms)
COMMIT

Some of the valid date values are in the params array, but are being filtered out before the SQL commits.

Here is the controller code:

  def create
    @workout = Workout.find(params[:workout][:id])

    7.times do |count|
      @schedule = params[:workout][:schedules_attributes]["#{count}"]
      if (@schedule[:scheduled_start_time].blank?)
        params[:workout][:schedules_attributes].delete count.to_s.to_sym
      end
    end

    if @workout.update_attributes(params[:workout])
      redirect_to schedules_url, :notice  => "Successfully updated schedule."
    else
      render :action => 'new'
    end
  end

And the Workout Model

class Workout < ActiveRecord::Base

belongs_to :team, :class_name => "Team", :foreign_key => "team_id"
has_many :exercise_instances, :dependent => :destroy

validates :name,
          :presence => true

has_many :schedules, :dependent => :destroy
accepts_nested_attributes_for :schedules

end

And the Schedule Model

class Schedule < ActiveRecord::Base
  attr_accessible :workout_id, :scheduled_start_time, :scheduled_end_time, :notes
  belongs_to :workout
end

Any direction would be welcome. I’m suspecting caching at some level, but I’m just not sure where to start looking. Thanks!

  • 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-23T11:19:20+00:00Added an answer on May 23, 2026 at 11:19 am

    I think you are reinventing the wheel here and it’s the source of the problem. Try the following:

    class Workout < ActiveRecord::Base
      has_many :schedules, :dependent => :delete_all
      accepts_nested_attributes_for :schedules, :allow_destroy => true, :reject_if => proc { |schedule| schedule[:scheduled_start_time].blank? }
    end
    

    You will still need to restrict the number of schedules to 7. I would recommend checking out existing solutions for this behavior; there are a lot of solid patterns on this already.

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

Sidebar

Related Questions

I keep finding that if I have nested divs inside each other, and one
I have a form for a nested resource (Client :has_many Workouts) that I want
I have a nested custom control that I need to set focus on. I
I currently have a form set up with nested models - all going according
I have a list of 'Interests' that each user in my system has the
I have a nested json. I want to post it as a form input
Say I have a Textbox nested within a TabControl . When the form loads,
I have the following form for photo_album which uses the nested forms feature of
I have nested xsl:for loops: <xsl:for-each select=/Root/A> <xsl:for-each select=/Root/B> <!-- Code --> </xsl:for> </xsl:for>
Is it possible to have nested set capabilities in this somewhat custom setup? Consider

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.