I''m trying to set up a directional self-referential HABTM to represent
an arbitrary semi-hierarchical structure; it works for any data
prepopulated into the db, but when I try to create a new relationship,
I just get NULLs in both key columns (or 0''s if I turn on the no-null
constraint).
Here are the migrations:
class CreateNodes < ActiveRecord::Migration
def self.up
create_table :nodes do |t|
t.column :name, :string
end
end
def self.down
drop_table :nodes
end
end
class CreateNodesNodes < ActiveRecord::Migration
def self.up
create_table :nodes_nodes, :id => false do |t|
t.column :parent_id, :integer
t.column :child_id, :integer
end
end
def self.down
drop_table :nodes_nodes
end
end
And here is the model:
class Node < ActiveRecord::Base
has_and_belongs_to_many :parents, :foreign_key => :child_id,
:association_foreign_key
=> :parent_id,
:class_name => ''Node'';
has_and_belongs_to_many :children, :foreign_key => :parent_id,
:association_foreign_key
=> :child_id,
:class_name =>
''Node'';
end
I''ve also tried swapping the foreign and association_foreign keys
around.
Any idea why this isn''t working? I''m using Rails 1.2.5 on
MySQL, and
aside from the above code and the database.yml I haven''t touched
anything that "rails app" created.> ruby script/console
Loading development environment.>> p = Node.new
=> #<Node:0xb7192348 @new_record=true,
@attributes={"name"=>nil}>>> p.name = ''parent''
=> "parent">> c = Node.new
=> #<Node:0xb714a8f4 @new_record=true,
@attributes={"name"=>nil}>>> c.name = ''child''
=> "child">> p.children << c
=> [#<Node:0xb714a8f4 @new_record=true,
@attributes={"name"=>"child"}>]>> p.children
=> [#<Node:0xb714a8f4 @new_record=true,
@attributes={"name"=>"child"}>]>> c.parents
=> []>> p.save
=> true>> c.save
=> true>> p.reload
=> #<Node:0xb7192348 @new_record_before_save=true, @children=nil,
@new_record=false, @attributes={"name"=>"parent",
"id"=>"1"},
@parents=nil, @errors=#<ActiveRecord::Errors:0xb71251a8 @errors={},
@base=#<Node:0xb7192348 ...>>>>> p.children
=> []>> c.reload
=> #<Node:0xb714a8f4 @children=nil, @new_record_before_save=false,
@new_record=false, @attributes={"name"=>"child",
"id"=>"2"},
@parents=nil, @errors=#<ActiveRecord::Errors:0xb7124f64 @errors={},
@base=#<Node:0xb714a8f4 ...>>>>> c.parents
=> []
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---