Hello Railslist, (I''m using RadRails 0.6.3 on Windows XP with Ruby 1.8.4, Rails 1.1 and SQLite3) I have two tables in the database: "locations" and "customers": customers.sql --- id INT PRIMARY KEY name VARCHAR 255 address VARCHAR 255 postal_code VARCHAR 255 city VARCHAR 255 --- locations.sql --- id INT PRIMARY KEY postal_code TEXT longitude FLOAT latitude FLOAT --- I''ve created two models (customer.rb and location.rb): customer.rb --- class Customer < ActiveRecord::Base belongs_to :location, :foreign_key=>''postal_code'' validates_presence_of :name, :postal_code end --- location.rb --- class Location < ActiveRecord::Base has_many :customers end --- The locations table is filled with >300,000 records. The customers table has a couple of records with postal_codes that match the locations table. In the controller, I have... --- def test @customer = Customer.find(1) end --- In the view, I have... --- <%= @customer.name %> --- ...which yields the first name-record in the customers table, so that''s OK. But shouldn''t I be able to now do something like: --- <%= @customer.location.latitude %> --- ...and expect it to return the corresponding latitude from the locations table? I get the nil error: --- "You have a nil object when you didn''t expect it! The error occured while evaluating nil.latitude" --- What could be wrong? Is it something with the way I''ve set up the alternative foreign-key? or is it a plural/singular naming problem? Thanks for any help. Gijs Nijholt
Replace the postal_code field in customers with a postal_code_id int field, then you should be able to get that working. -N On 24/05/06, Gijs Nijholt <gijs.nijholt@gmail.com> wrote:> Hello Railslist, > > (I''m using RadRails 0.6.3 on Windows XP with Ruby 1.8.4, Rails 1.1 and SQLite3) > > I have two tables in the database: "locations" and "customers": > > customers.sql > --- > id INT PRIMARY KEY > name VARCHAR 255 > address VARCHAR 255 > postal_code VARCHAR 255 > city VARCHAR 255 > --- > > locations.sql > --- > id INT PRIMARY KEY > postal_code TEXT > longitude FLOAT > latitude FLOAT > --- > > > I''ve created two models (customer.rb and location.rb): > > customer.rb > --- > class Customer < ActiveRecord::Base > belongs_to :location, :foreign_key=>''postal_code'' > validates_presence_of :name, :postal_code > end > --- > > location.rb > --- > class Location < ActiveRecord::Base > has_many :customers > end > --- > > The locations table is filled with >300,000 records. > The customers table has a couple of records with postal_codes that > match the locations table. > > In the controller, I have... > --- > def test > @customer = Customer.find(1) > end > --- > > In the view, I have... > --- > <%= @customer.name %> > --- > ...which yields the first name-record in the customers table, so that''s OK. > > But shouldn''t I be able to now do something like: > --- > <%= @customer.location.latitude %> > --- > ...and expect it to return the corresponding latitude from the locations table? > > I get the nil error: > --- > "You have a nil object when you didn''t expect it! > The error occured while evaluating nil.latitude" > --- > > What could be wrong? > Is it something with the way I''ve set up the alternative foreign-key? > or is it a plural/singular naming problem? > > Thanks for any help. > > Gijs Nijholt > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
does the table postal_code_in need to be of type INT per se? I still get exactly the same error (nil object) as before (I restarted webrick) On 5/24/06, njmacinnes@gmail.com <njmacinnes@gmail.com> wrote:> Replace the postal_code field in customers with a postal_code_id int > field, then you should be able to get that working. > -N > > On 24/05/06, Gijs Nijholt <gijs.nijholt@gmail.com> wrote: > > Hello Railslist, > > > > (I''m using RadRails 0.6.3 on Windows XP with Ruby 1.8.4, Rails 1.1 and SQLite3) > > > > I have two tables in the database: "locations" and "customers": > > > > customers.sql > > --- > > id INT PRIMARY KEY > > name VARCHAR 255 > > address VARCHAR 255 > > postal_code VARCHAR 255 > > city VARCHAR 255 > > --- > > > > locations.sql > > --- > > id INT PRIMARY KEY > > postal_code TEXT > > longitude FLOAT > > latitude FLOAT > > --- > > > > > > I''ve created two models (customer.rb and location.rb): > > > > customer.rb > > --- > > class Customer < ActiveRecord::Base > > belongs_to :location, :foreign_key=>''postal_code'' > > validates_presence_of :name, :postal_code > > end > > --- > > > > location.rb > > --- > > class Location < ActiveRecord::Base > > has_many :customers > > end > > --- > > > > The locations table is filled with >300,000 records. > > The customers table has a couple of records with postal_codes that > > match the locations table. > > > > In the controller, I have... > > --- > > def test > > @customer = Customer.find(1) > > end > > --- > > > > In the view, I have... > > --- > > <%= @customer.name %> > > --- > > ...which yields the first name-record in the customers table, so that''s OK. > > > > But shouldn''t I be able to now do something like: > > --- > > <%= @customer.location.latitude %> > > --- > > ...and expect it to return the corresponding latitude from the locations table? > > > > I get the nil error: > > --- > > "You have a nil object when you didn''t expect it! > > The error occured while evaluating nil.latitude" > > --- > > > > What could be wrong? > > Is it something with the way I''ve set up the alternative foreign-key? > > or is it a plural/singular naming problem? > > > > Thanks for any help. > > > > Gijs Nijholt > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
1. postal_code column in both the tables should be of same type if that is not working then having a location_id column in customers table, referring to id column of locations table will surely work; with this you can skip :foreign_key option, Rails will take care of that. Regards, Jatinder On 5/24/06, Gijs Nijholt <gijs.nijholt@gmail.com> wrote:> > does the table postal_code_in need to be of type INT per se? > I still get exactly the same error (nil object) as before (I restarted > webrick) > > > On 5/24/06, njmacinnes@gmail.com <njmacinnes@gmail.com> wrote: > > Replace the postal_code field in customers with a postal_code_id int > > field, then you should be able to get that working. > > -N > > > > On 24/05/06, Gijs Nijholt <gijs.nijholt@gmail.com> wrote: > > > Hello Railslist, > > > > > > (I''m using RadRails 0.6.3 on Windows XP with Ruby 1.8.4, Rails 1.1 and > SQLite3) > > > > > > I have two tables in the database: "locations" and "customers": > > > > > > customers.sql > > > --- > > > id INT PRIMARY KEY > > > name VARCHAR 255 > > > address VARCHAR 255 > > > postal_code VARCHAR 255 > > > city VARCHAR 255 > > > --- > > > > > > locations.sql > > > --- > > > id INT PRIMARY KEY > > > postal_code TEXT > > > longitude FLOAT > > > latitude FLOAT > > > --- > > > > > > > > > I''ve created two models (customer.rb and location.rb): > > > > > > customer.rb > > > --- > > > class Customer < ActiveRecord::Base > > > belongs_to :location, :foreign_key=>''postal_code'' > > > validates_presence_of :name, :postal_code > > > end > > > --- > > > > > > location.rb > > > --- > > > class Location < ActiveRecord::Base > > > has_many :customers > > > end > > > --- > > > > > > The locations table is filled with >300,000 records. > > > The customers table has a couple of records with postal_codes that > > > match the locations table. > > > > > > In the controller, I have... > > > --- > > > def test > > > @customer = Customer.find(1) > > > end > > > --- > > > > > > In the view, I have... > > > --- > > > <%= @customer.name %> > > > --- > > > ...which yields the first name-record in the customers table, so > that''s OK. > > > > > > But shouldn''t I be able to now do something like: > > > --- > > > <%= @customer.location.latitude %> > > > --- > > > ...and expect it to return the corresponding latitude from the > locations table? > > > > > > I get the nil error: > > > --- > > > "You have a nil object when you didn''t expect it! > > > The error occured while evaluating nil.latitude" > > > --- > > > > > > What could be wrong? > > > Is it something with the way I''ve set up the alternative foreign-key? > > > or is it a plural/singular naming problem? > > > > > > Thanks for any help. > > > > > > Gijs Nijholt > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060525/9127a086/attachment-0001.html
Oh yeah, that''s what I meant. I was thinking you had a postal_codes table, because I''ve got one of those. But using an int location_id would do the job. -N On 25/05/06, jatinder saundh <jatinder.saundh@gmail.com> wrote:> > 1. postal_code column in both the tables should be of same type > if that is not working then having a location_id column in customers table, > referring to id column of locations table will surely work; with this you > can skip :foreign_key option, Rails will take care of that. > > Regards, > Jatinder > > > On 5/24/06, Gijs Nijholt <gijs.nijholt@gmail.com> wrote: > > does the table postal_code_in need to be of type INT per se? > > I still get exactly the same error (nil object) as before (I restarted > webrick) > > > > > > On 5/24/06, njmacinnes@gmail.com <njmacinnes@gmail.com> wrote: > > > Replace the postal_code field in customers with a postal_code_id int > > > field, then you should be able to get that working. > > > -N > > > > > > On 24/05/06, Gijs Nijholt <gijs.nijholt@gmail.com> wrote: > > > > Hello Railslist, > > > > > > > > (I''m using RadRails 0.6.3 on Windows XP with Ruby 1.8.4, Rails 1.1 and > SQLite3) > > > > > > > > I have two tables in the database: "locations" and "customers": > > > > > > > > customers.sql > > > > --- > > > > id INT PRIMARY KEY > > > > name VARCHAR 255 > > > > address VARCHAR 255 > > > > postal_code VARCHAR 255 > > > > city VARCHAR 255 > > > > --- > > > > > > > > locations.sql > > > > --- > > > > id INT PRIMARY KEY > > > > postal_code TEXT > > > > longitude FLOAT > > > > latitude FLOAT > > > > --- > > > > > > > > > > > > I''ve created two models (customer.rb and location.rb): > > > > > > > > customer.rb > > > > --- > > > > class Customer < ActiveRecord::Base > > > > belongs_to :location, :foreign_key=>''postal_code'' > > > > validates_presence_of :name, :postal_code > > > > end > > > > --- > > > > > > > > location.rb > > > > --- > > > > class Location < ActiveRecord::Base > > > > has_many :customers > > > > end > > > > --- > > > > > > > > The locations table is filled with >300,000 records. > > > > The customers table has a couple of records with postal_codes that > > > > match the locations table. > > > > > > > > In the controller, I have... > > > > --- > > > > def test > > > > @customer = Customer.find(1) > > > > end > > > > --- > > > > > > > > In the view, I have... > > > > --- > > > > <%= @customer.name %> > > > > --- > > > > ...which yields the first name-record in the customers table, so > that''s OK. > > > > > > > > But shouldn''t I be able to now do something like: > > > > --- > > > > <%= @customer.location.latitude %> > > > > --- > > > > ...and expect it to return the corresponding latitude from the > locations table? > > > > > > > > I get the nil error: > > > > --- > > > > "You have a nil object when you didn''t expect it! > > > > The error occured while evaluating nil.latitude" > > > > --- > > > > > > > > What could be wrong? > > > > Is it something with the way I''ve set up the alternative foreign-key? > > > > or is it a plural/singular naming problem? > > > > > > > > Thanks for any help. > > > > > > > > Gijs Nijholt > > > > > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails@lists.rubyonrails.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >