Hi all, I am trying to map a recursive data model in rails and can''t seem to get it to work. here is the SQL model : CREATE TABLE maplocations( id INTEGER AUTOINCREMENT PRIMARY KEY, coordx INTEGER NOT NULL, coordy INTEGER NOT NULL, filename VARCHAR(20) NOT NULL, linkup INTEGER DEFAULT -1, linkdown INTEGER DEFAULT -1, linknorth INTEGER DEFAULT -1, linkeast INTEGER DEFAULT -1, linksouth INTEGER DEFAULT -1, linkwest INTEGER DEFAULT -1 ); INSERT INTO maplocations VALUES(1,0,0,''test.gif'',-1,-1,-1,-1,-1,-1); INSERT INTO maplocations VALUES(2,1,1,''test2.gif'',-1,-1,-1,-1,-1,-1); INSERT INTO maplocations VALUES(3,1,1,''test2.gif'',-1,-1,1,-1,2,-1); As you may guess, I want linkup, linkdown,... to refer to other lines in maplocations. after creating this, I did the following : $>ruby script\generate model Maplocation $>ruby script\generate controller maplocation then edited the model so it looked like this : class Maplocation < ActiveRecord::Base belongs_to :maplocation, :foreign_key=> "linkup" belongs_to :maplocation, :foreign_key=> "linkdown" belongs_to :maplocation, :foreign_key=> "linkeast" belongs_to :maplocation, :foreign_key=> "linkwest" belongs_to :maplocation, :foreign_key=> "linknorth" belongs_to :maplocation, :foreign_key=> "linksouth" end (I tried this after reading the Active Record documentation) changed the controller so it looked like this : class MaplocationController < ApplicationController model :maplocation def index render_text("ok") end def test @maproot=Maplocation.new("id" => "3") end end and added a test.rhtml file in app/views/maplocation : <html> <head> <title>test recurse</title> </head> <body> <%= @maproot.coordx %><br> <%= @maproot.coordy %> <br> <%= @maproot.linkup.coordx %><br> <%= @maproot.linkdown.coordy %><br> </body> </html> and now I get the following : NoMethodError in Maplocation#test Showing /maplocation/test.rhtml where line #8 raised undefined method `coordx'' for -1:Fixnum 5: <body> 6: <%= @maproot.coordx %><br> 7: <%= @maproot.coordy %> <br> 8: <%= @maproot.linkup.coordx %><br> 9: <%= @maproot.linkdown.coordy %><br> 10: </body> 11: </html> Which of course isn''t what I want. I would like it to be able to convert the link into an object. is that possible, if so : how ? (I attached the files in case the copy paste doest mail correctly, and I use SQLITE 2.8 and the respective bindings) thx Jean <DISCLAIMER> I am completely new to rails and mostly new to ruby, I have succeeded at running the tutorial and I am experimenting a bit with rails, and might be trying to do stupid things. If so, please show me a better way. </DISCLAIMER> _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Tue, 1 Feb 2005 18:47:05 +0100, Jean Helou <jean.helou-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class Maplocation < ActiveRecord::Base > belongs_to :maplocation, :foreign_key=> "linkup" > belongs_to :maplocation, :foreign_key=> "linkdown" > belongs_to :maplocation, :foreign_key=> "linkeast" > belongs_to :maplocation, :foreign_key=> "linkwest" > belongs_to :maplocation, :foreign_key=> "linknorth" > belongs_to :maplocation, :foreign_key=> "linksouth" > endTry this: class Maplocation < ActiveRecord::Base belongs_to :linkup, :class_name => ''Maplocation'', :foreign_key=> "linkup_id" belongs_to :linkdown, :class_name => ''Maplocation'', :foreign_key=> "linkdown_id" belongs_to :linkeast, :class_name => ''Maplocation'', :foreign_key=> "linkeast_id" belongs_to :linkwest, :class_name => ''Maplocation'', :foreign_key=> "linkwest_id" belongs_to :linknorth, :class_name => ''Maplocation'', :foreign_key=> "linknorth_id" belongs_to :linksouth, :class_name => ''Maplocation'', :foreign_key=> "linksouth_id" end And add ''_id'' to the column names of those foreign keys in your DB schema. -- Regards, John Wilger ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don''t know," Alice answered. "Then," said the cat, "it doesn''t matter." - Lewis Carrol, Alice in Wonderland
On Tue, 1 Feb 2005 14:42:24 -0500, John Wilger <johnwilger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Tue, 1 Feb 2005 18:47:05 +0100, Jean Helou <jean.helou-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > class Maplocation < ActiveRecord::Base > > belongs_to :maplocation, :foreign_key=> "linkup" > > belongs_to :maplocation, :foreign_key=> "linkdown" > > belongs_to :maplocation, :foreign_key=> "linkeast" > > belongs_to :maplocation, :foreign_key=> "linkwest" > > belongs_to :maplocation, :foreign_key=> "linknorth" > > belongs_to :maplocation, :foreign_key=> "linksouth" > > end > > Try this: > > class Maplocation < ActiveRecord::Base > belongs_to :linkup, :class_name => ''Maplocation'', :foreign_key=> "linkup_id" > belongs_to :linkdown, :class_name => ''Maplocation'', :foreign_key=> > "linkdown_id" > belongs_to :linkeast, :class_name => ''Maplocation'', :foreign_key=> > "linkeast_id" > belongs_to :linkwest, :class_name => ''Maplocation'', :foreign_key=> > "linkwest_id" > belongs_to :linknorth, :class_name => ''Maplocation'', :foreign_key=> > "linknorth_id" > belongs_to :linksouth, :class_name => ''Maplocation'', :foreign_key=> > "linksouth_id" > end > > And add ''_id'' to the column names of those foreign keys in your DB schema.Thanks for the answer, just for the record it wasn''t completely enough but it was sufficient to help me understand a few things and correct the stuff :) After I tried it the first time, I saw that I was being stupid, since I was trying to access linkup and linkdown while my db only contained links for north and south :) I changed the rhtml accordingly Then I tried again and got this : Showing /maplocation/test.rhtml where line #8 raised undefined method `coordx'' for nil:NilClass 5: <body> 6: <%= @maproot.coordx %><br> 7: <%= @maproot.coordy %> <br> 8: <%= @maproot.linknorth.coordx %><br> 9: <%= @maproot.linksouth.coordy %><br> 10: </body> 11: </html> Now I get the problem but it puzzeld me for some time :) the problem is in the controller, my test method was like this : def test @maproot=Maplocation.new("id" => "3") end This doesn''t work because new will not go fetch the data in the DB. It will only create a new object with this id (inserting this one would porobably fail, haven''t had the time to test it). I have to use find instead : def test @maproot=Maplocation.find(3) end Now this works. Thanks Jean