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
--
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.
On 23 March 2010 18:05, David Ishmael <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 => "NodeGroup", > :foreign_key => "child_id" > > has_one :parent, :through => :parentsThat 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.ymlShould be node_groups.yml> parent1: > name: Parent1 > children: child1I would have expected this to be parent: Parent1 child: child1 Colin> > parent2: > name: Parent2 > > child1: > name: Child1 > -- > 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@googlegroups.com. > 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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? -- 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.
On 24 March 2010 10:51, David Ishmael <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 -- 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.
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.
--
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 have snipped everything again so that it will make it difficult for people to follow the thread. On 24 March 2010 11:25, David Ishmael <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 => :parentsAs 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 -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
That worked perfectly, thanks! -- 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.
On 24 March 2010 12:48, David Ishmael <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 -- 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.
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. -- 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.