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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Tue, Jul 19, 2011 at 10:17 AM, Matt Garland <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.