Associated child records not created on creation of a parent


#1

I am trying to create a record with association. Within the create
method, I would also like to pre-populate dependent (has_many) records
from a template in the database.

The child records are mysteriously rolled back if I insert
them in a loop. If I try to insert a single child record, it works.I
have read the books and tried to google my way out of this but am at a
dead end. Any help is appreciated.

Problem Statement ------------
PrototypeModel has many Departments .

 Departments may also be grouped within Departments.

Trying to create a new PrototypeModel and populating the departments
(behind the scenes) from a template PrototypeModel specified by the
user in params[:prototype_model][:parent_id].

The save of the PrototypeModel fails as the new Department records are
invalid.
I checked the instances and find that the department.id is
nil. But that id is supposed to be populated by the associated
save! If I replace the departments.build argument in the
controller::create with a set of static arguments like :name => ‘Test
name’, :description=> ‘Test Desc’ then it works. though only one
hard code department is created :frowning:

Just cant get my head around this. Code is below:

Code :

  1. class PrototypeModel < ActiveRecord::Base
  2. belongs_to :author, :class_name => ‘User’
  3. has_many :departments
  4. belongs_to :prototype_model
  5. acts_as_tree :foreign_key => “parent_id”
  6. end

Code :

  1. class Department < ActiveRecord::Base
  2. validates_presence_of :name, :description
  3. validates_presence_of :parent_id
  4. acts_as_tree :foreign_key => “parent_id”
  5. belongs_to :department
  6. belongs_to :prototype_model
  7. end

Code :

  1. POST /prototype_models

  2. POST /prototype_models.xml

  3. def create
  4. @template_model = PrototypeModel.find( params
    

[:prototype_model][:parent_id] )
6. template_departments = @template_model.departments
7.
8. @prototype_model = PrototypeModel.new(params
[:prototype_model])
9. @prototype_model.author = current_user
10.
11. for mydepartment in template_departments
12. logger.debug “Cloning Department #{mydepartment.name}”
13.
14. newDepartment= @prototype_model.departments.build( :name =>
mydepartment.name, :description=>
mydepartment.description, :parent_id=> mydepartment.parent_id )
15. end
16.
17. respond_to do |format|
18. if @prototype_model.save
19. flash[:notice] = ‘PrototypeModel was successfully
created.’
20. format.html { redirect_to(@prototype_model) }
21. format.xml { render :xml => @prototype_model, :status
=> :created, :location => @prototype_model }
22. else
23. format.html { render :action => “new” }
24. format.xml { render :xml =>
@prototype_model.errors, :status => :unprocessable_entity }
25. end
26. end
27. end

I am using Rails 2.2.2 and Ruby 1.8.6

thanks


#2

Master Chief wrote:

I am trying to create a record with association. Within the create
method, I would also like to pre-populate dependent (has_many) records
from a template in the database.

I’m not exactly clear what you’re doing, but here are some
comments that may help.

  1. acts_as_tree :foreign_key => “parent_id”
  2. belongs_to :department
  3. belongs_to :prototype_model
  4. end
  1. Are the two belongs_to associations that reference their
    own class necessary, or can you use the parent association
    generated by acts_as_tree?

  2. You should use “validates_presence_of :parent” instead.
    Using the foreign key will prevent validation when the
    parent is new. You have to make sure that either
    (1), the foreign-key is non-NULL and points to an existing
    record, or (2), that a new parent object is assigned to the
    association.


Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com


#3

Resolved!
One of the data sets inserted thru SQL as a template was not passing
the Validates_presence_of clauses so it was failing to insert the
association.

Thats why my static data was working.

Also changed the validates_presence_of: parent_id to
validates_presence_of: parent

Also removed the self reference in the belongs_to…

Thanks for the nudge in the right direction! Now I can get back on
track… :slight_smile:



#4

Thank you for your response!

  1. I didnt know acts_as_tree generated an association. Removed parent
    association from both classes.
  2. Changed the validates_presence_of clause.

It still gave me the same errors. :frowning:

New Info:
I also tried after removing the vpof clause altogether in
departments. It removed a bunch of parent queries from the log. What
remained was just a BEGIN and ROLLBACK but no error in the log. And
in the flash area I still got the message that the department is
invalid, so it cannot save the prototype_model.

Going to add some more logging to see if I get any more info.
Have been stuck here for 3 days, so really appreciate your help!

regards