1

I'm trying to save a record of a class being added to a schedule, then take the start date and add a week at a time adding additional entries into the database until I reach the end date. Here is my controller method

def schedule_class
  @program_schedule = ProgramSchedule.new(params[:program_schedule])

  if @program_schedule.save
    @program_schedule.schedule_classes
  end
end

So if I successfully save the model I call this method in the model

def schedule_classes
  class_start = Date.parse(self.first_date, '%m/%d/%Y')
  until class_start > self.last_date.to_date
  schedule = Schedule.new
    schedule.program_schedule_id = self.id
    schedule.start_date = class_start.to_s + ' ' + self.start_time
    schedule.end_date = class_start.to_s +' ' + self.end_time
    schedule.deleted = false

    schedule.save

    class_start.to_time.advance(:week => 1).to_date
  end
end

The error I get is $_ value need to be String (nil given)

I get that error on this line: class_start = Date.parse(self.first_date, '%m/%d/%Y')

And these are the parameters in program_schedule

{"commit"=>"Schedule Class",
  "authenticity_token"=>"MRtbXnPNq/xLr74awXfS4ksG/NK92aLwsogC8R8IHB0=",
  "utf8"=>"✓",
  "program_schedule"=>{"program_id"=>"1",
  "end_time"=>"01:00 am",
  "last_date"=>"08/25/2011",
  "first_date"=>"07/21/2011",
  "week_day_id"=>"5",
"start_time"=>"12:00 am"}}

Since the model has all it's values I'm not sure where the nil value error is coming from.

3
  • Following may help you stackoverflow.com/questions/6475013/… Commented Jul 22, 2011 at 4:49
  • Are you storing first_date as a datetime or string? Commented Jul 22, 2011 at 5:13
  • Looks like I'm storing it as a datetime in the database. Commented Jul 22, 2011 at 5:35

1 Answer 1

0

I fixed it using the following

class_start = DateTime.parse(self.first_date.to_date.to_s + ' ' + self.start_time.strftime('%I:%M%p'))

Since they were datetime objects I needed to strip the date out of one and the time out of the other to create the actual datetime I needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.