Fixtures and Associations

I have a concrete table with an associated join table and I'm trying to
load up some test data but cannot seem to get the fixtures file correct.
The parent/child relationship works great but the fixtures fail to load.
Any help would be greatly appreciated.

Here is what I have...

# app/models/node.rb
class Node < ActiveRecord::Base
  belongs_to :category
  belongs_to :status

  has_one :parents,
           :class_name => "NodeGroup",
           :foreign_key => "child_id"

  has_one :parent, :through => :parents

  has_many :children,
           :class_name => "NodeGroup",
           :foreign_key => "parent_id"

end

# app/models/node_group.rb
class NodeGroup < ActiveRecord::Base
  belongs_to :parent, :class_name => "Node"
  belongs_to :child, :class_name => "Node"
end

# test/fixtures/nodes.yml
parent1:
  name: Parent1
  children: child1

parent2:
  name: Parent2

child1:
  name: Child1

I have a concrete table with an associated join table and I'm trying to
load up some test data but cannot seem to get the fixtures file correct.
The parent/child relationship works great but the fixtures fail to load.
Any help would be greatly appreciated.

Here is what I have...

# app/models/node.rb
class Node < ActiveRecord::Base
belongs_to :category
belongs_to :status

has_one :parents,

Why is that not has_one :parent ?

      :class\_name =&gt; &quot;NodeGroup&quot;,
      :foreign\_key =&gt; &quot;child\_id&quot;

has_one :parent, :through => :parents

That would only work if there were a relationship to a parents table

has_many :children,
:class_name => "NodeGroup",
:foreign_key => "parent_id"

end

# app/models/node_group.rb
class NodeGroup < ActiveRecord::Base
belongs_to :parent, :class_name => "Node"
belongs_to :child, :class_name => "Node"
end

# test/fixtures/nodes.yml

Should be node_groups.yml

parent1:
name: Parent1
children: child1

I would have expected this to be
  parent: Parent1
  child: child1

Colin

I added the entries to the test/fixtures/node_groups.yml file and that
seems to have worked - thanks. For some reason I thought I could link
the nodes through the test/fixtures/nodes.yml file.

As for the relationship, the Node can have one or more children but only
one parent. The idea is that I can do something like this...

foo = Node.new(:name => "foo")
bar = Node.new(:name => "bar")

# Add a parent
bar.parent = foo

# Show the children
foo.children

This seems to work; is there a better way?

A better way than what? You have snipped everything and it is not
clear to me what your current relationships are.

Colin

To clarify...

Node is a self-referential object. Here is the class for the Node
object.

class Node < ActiveRecord::Base
  belongs_to :category
  belongs_to :status

  has_one :parents,
           :class_name => "NodeGroup",
           :foreign_key => "child_id"
  has_one :parent, :through => :parents

  has_many :children,
           :class_name => "NodeGroup",
           :foreign_key => "parent_id"
end

And here is the class for the NodeGroup.

class NodeGroup < ActiveRecord::Base
  belongs_to :parent, :class_name => "Node"
  belongs_to :child, :class_name => "Node"
end

The relationship is that Node can have one parent or many children.

You have snipped everything again so that it will make it difficult
for people to follow the thread.

To clarify...

Node is a self-referential object. Here is the class for the Node
object.

class Node < ActiveRecord::Base
belongs_to :category
belongs_to :status

has_one :parents,
:class_name => "NodeGroup",
:foreign_key => "child_id"
has_one :parent, :through => :parents

As I asked in my earlier post why have you not just got
has_one :parent, # singular
          :class_name => "NodeGroup",
          :foreign_key => "child_id"

I don't understand what has_one :parents and then has_one :parent,
:through :parents is supposed to mean. How can it have one parent and
one parents?

has_many :children,
:class_name => "NodeGroup",
:foreign_key => "parent_id"
end

And here is the class for the NodeGroup.

class NodeGroup < ActiveRecord::Base
belongs_to :parent, :class_name => "Node"
belongs_to :child, :class_name => "Node"
end

The relationship is that Node can have one parent or many children.

Actually as I look at it I realise that I think you are down the wrong
track anyway. As you have it the parent and child of Node are
NodeGroups, which is not self referential. You don't need the
NodeGroup at all. You just want something like
(for the node) (untested)
belongs_to: parent, :class_name => "Node", :foreign_key => "parent_id"
has_many: children, :class_name => "Node"

That way each Node has a parent_id that references another Node, that
is its parent. There may be many nodes with the same parent_id so you
have the situation where a node has one parent but can have many
children.

Colin

That worked perfectly, thanks!

What did? It is not clear which email you are responding to as you
have snipped it all.

You might save some time by looking at acts_as_tree or probably even
better awsome_nested_set.

Colin

I modified the Node class using the code you provided, updated the model
in the database, and then tested with success. To be clear, I am
referring to your code below.

belongs_to: parent, :class_name => "Node", :foreign_key => "parent_id"
has_many: children, :class_name => "Node"

Everything appears to be working correctly. Again, thanks for the help.