Hello, I hope someone can help with this "belongs_to" problem. I am unable to instantiate model objects of that class that says "belongs_to". I am getting a warning saying "You have a nil object when you probably didn''t expect it! Odds are you want an instance of Array instead." The activerecords code that raises the error is active_record/base.rb:1210:in `read_attribute'' (see attached callstack). I have just two models: Query and Connection, where each query is associated with a connection. The queries table contains a column "connection_id" pointing at a row in table "connections". The Query model class contains a "belongs_to" relationship: class Query < ActiveRecord::Base belongs_to :connection validates_presence_of :name, :sql end The Connection model class contains no relationship, since I have no need to navigate the relationship in this direction: class Connection < ActiveRecord::Base validates_presence_of :name, :host end I can successfully find a query object, but I can''t instantiate one. ActiveRecords fails in line 1210 of base.rb when method read_attributes() is called with attribute name "connection_id" (the foreign key attribute). Instance variable @attributes evaluates to nothing. Is this a bug in ActiveRecords or am I doing something wrong? I hope someone can help me with this. I am using Ruby 1.8.3, Windows XP, cygwin, MySQL 4.1.14-nt, and Rails 0.13.1. Thanks in advance, -Michael _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
ActiveRecord reserves the word ''connection'' as a method that returns a live connection to the database. I betcha your "belongs_to :connection" statement is doing weird things to AR by re- declaring ''connection'' as an association instead. Duane Johnson (canadaduane) On Oct 8, 2005, at 10:36 AM, Michael Nestler wrote:> Hello, > > I hope someone can help with this "belongs_to" problem. I am unable to > instantiate model objects of that class that says "belongs_to". I am > getting a warning saying "You have a nil object when you probably > didn''t expect it! Odds are you want an instance of Array instead." > > The activerecords code that raises the error is > active_record/base.rb:1210:in `read_attribute'' (see attached > callstack). > > I have just two models: Query and Connection, where each query is > associated with a connection. The queries table contains a column > "connection_id" pointing at a row in table "connections". The Query > model class contains a "belongs_to" relationship: > > class Query < ActiveRecord::Base > belongs_to :connection > validates_presence_of :name, :sql > end > > The Connection model class contains no relationship, since I have no > need to navigate the relationship in this direction: > > class Connection < ActiveRecord::Base > validates_presence_of :name, :host > end > > I can successfully find a query object, but I can''t instantiate one. > ActiveRecords fails in line 1210 of base.rb when method > read_attributes() is called with attribute name "connection_id" (the > foreign key attribute). Instance variable @attributes evaluates to > nothing. > > Is this a bug in ActiveRecords or am I doing something wrong? I hope > someone can help me with this. > > I am using Ruby 1.8.3, Windows XP, cygwin, MySQL 4.1.14-nt, and > Rails 0.13.1. > > Thanks in advance, > -Michael > > <callstack.txt> > <database.sql> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thanks Duane, you were absolutely right. I suspected that it has something to do with a reserved word, but hoped that ActiveRecord would catch this kind of problem when analyzing table metadata. At least now I know what kind of error message I can expect in this situation. -Michael On 10/8/05, Duane Johnson <duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ActiveRecord reserves the word ''connection'' as a method that returns > a live connection to the database. I betcha your > "belongs_to :connection" statement is doing weird things to AR by re- > declaring ''connection'' as an association instead. > > Duane Johnson > (canadaduane) > > On Oct 8, 2005, at 10:36 AM, Michael Nestler wrote: > > > Hello, > > > > I hope someone can help with this "belongs_to" problem. I am unable to > > instantiate model objects of that class that says "belongs_to". I am > > getting a warning saying "You have a nil object when you probably > > didn''t expect it! Odds are you want an instance of Array instead." > > > > The activerecords code that raises the error is > > active_record/base.rb:1210:in `read_attribute'' (see attached > > callstack). > > > > I have just two models: Query and Connection, where each query is > > associated with a connection. The queries table contains a column > > "connection_id" pointing at a row in table "connections". The Query > > model class contains a "belongs_to" relationship: > > > > class Query < ActiveRecord::Base > > belongs_to :connection > > validates_presence_of :name, :sql > > end > > > > The Connection model class contains no relationship, since I have no > > need to navigate the relationship in this direction: > > > > class Connection < ActiveRecord::Base > > validates_presence_of :name, :host > > end > > > > I can successfully find a query object, but I can''t instantiate one. > > ActiveRecords fails in line 1210 of base.rb when method > > read_attributes() is called with attribute name "connection_id" (the > > foreign key attribute). Instance variable @attributes evaluates to > > nothing. > > > > Is this a bug in ActiveRecords or am I doing something wrong? I hope > > someone can help me with this. > > > > I am using Ruby 1.8.3, Windows XP, cygwin, MySQL 4.1.14-nt, and > > Rails 0.13.1. > > > > Thanks in advance, > > -Michael > > > > <callstack.txt> > > <database.sql> > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >