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
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”)
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?
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.
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.