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=> "<application></application>" 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: "<application></application>", 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