Sorry if this has already been asked time and again. But after
spending a couple of hours reading the FAQ, forums and Googling, I
could not find any answer.

Here is my situation:

I have a form to create new courses. In this form, I have a Dates
section to record multiple instances of each course.
I rely on an embedded scaffold to display a subform where users can
add, update or delete course dates.


Here are my two models:

class Course < ActiveRecord::Base
  has_many :course_dates, :dependent => :destroy

end

class Instance < ActiveRecord::Base
  belongs_to :course
  # This next line is obviously part of the problem
  validates_presence_of :course_id
end


My controllers are as follows:

class CourseController < ApplicationController
  # Some standard code to deal with CRUD actions
  # No AS here
end

class InstanceController < ApplicationController
  # This is needed to prevent an exception when the sub-form doesn't
validate
  protect_from_forgery :only => [:create, :update, :delete]

  active_scaffold :instance do |config|
    config.actions.exclude :show, :search
    config.create.label = "Add a new date for this course"
    config.create.link.label = "Add a date"

    # Columns to display in the sub-table
    config.columns = [:course_start, :course_end]
end


Finally, in my Course view I embed the Instances table using this
line:

  <%= render :active_scaffold => 'instance', :label =>
'Dates', :constraints => { :course_id => @course.id } -%>


That works fine for an existing course. But if I want to create a new
course and enter the dates, I receive an error message "Course cannot
be blank". That makes sense because I assume that AS is trying to save
a child record to the database while the parent record (the course)
has not been saved yet (and might never be if the user decides to
cancel the creation).

I can remove the validation from the Instance model. That gets rid of
the error message and lets me create dates for the new course. But
then, they are not associated with the parent and I end up with a
bunch of orphaned records with their course_id = NULL

Is there a way to solve this in AS or do I need to use
"before_create_save" or some callback to save the course record before
saving the date record?

Any help greatly appreciated.

Christian.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/activescaffold?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to