Associations seemed saved in console, but aren't

I have a Course model:

class Course < ActiveRecord::Base

belongs_to :interaction_outline, :class_name => “Tree”, :foreign_key
=> “interaction_outline_id”
belongs_to :token_outline, :class_name => “Tree”, :foreign_key =>
“token_outline_id”

after_save :make_outlines

def make_outlines
self.create_interaction_outline :name=>
self.name+"_interaction_outline"
self.create_token_outline :name=> self.name+"_token_outline"
end

end

After it is saved, the course creates some default outlines/trees. These
in turn create some defaults:

class Tree < ActiveRecord::Base

has_many :edit_lists, :dependent=> :destroy
has_many :definitions, :dependent=> :destroy
has_many :courses
belongs_to :tree_node #the top node

validates_presence_of :name
validates_uniqueness_of :name

after_save :make_top

def make_top
self.create_tree_node
self.tree_node.create_definition :name=>“top”, :content=>
“”
end

end

The app is basically an outline editor that swaps in xml values for
outline items.

When I create a course in the console, all the defaults are created and
associations set:

c=Course.create :name=>“foo”
=> #<Course id: 5, name: “foo”, interaction_outline_id: 14,
token_outline_id: 15, created_at: “2011-07-19 17:11:18”, updated_at:
“2011-07-19 17:11:18”>

c
=> #<Course id: 5, name: “foo”, interaction_outline_id: 14,
token_outline_id: 15, created_at: “2011-07-19 17:11:18”, updated_at:
“2011-07-19 17:11:18”>

c.interaction_outline
=> #<Tree id: 14, name: “foo_interaction_outline”, tree_node_id: 14,
created_at: “2011-07-19 17:11:18”, updated_at: “2011-07-19 17:11:18”>

c.interaction_outline.tree_node
=> #<TreeNode id: 14, tree_id: nil, definition_id: 10, ancestry: nil,
position: nil, created_at: “2011-07-19 17:11:18”, updated_at:
“2011-07-19 17:11:18”>

c.interaction_outline.tree_node.definition
=> #<Definition id: 10, name: “top”, content:
“”, tree_id: 14, created_at: “2011-07-19
17:11:18”, updated_at: “2011-07-19 17:11:18”>

HOWEVER, they are not actually saved:

Course.all
=> [#<Course id: 5, name: “foo”, interaction_outline_id: nil,
token_outline_id: nil, created_at: “2011-07-19 17:11:18”, updated_at:
“2011-07-19 17:11:18”>]
What?

This is the value returned by my controllers, too, with no foreign keys
and thus no includes possible. Why?

I have confirmed that all my composed objects are valid.

Thanks!

Matt

After some research, I moved the creation of the associated objects to
the controller#create, where it belongs, really, and the code worked.

I was getting contradictory results in the console because I was using
the after_save filter. At first, the various builds/associations done in
the after_save filter are reflected in the status of the record in the
console. However, the associations/foreign keys were NOT saved since
they were created after_save. So when I called up the record from the db
again (Course.all), they were gone–tho the associated objects
themselves were in the db.

My first guess would be that it has to do with the definitions for the
following methods:

create_tree_node
tree_node.create_definition

What happens if, after the IRB code you pasted in, you call

c.save
Course.all

(if that doesn’t work, does the following work?)

c.interaction_outline.save
c.interaction_outline.tree_node.save
Course.all