[Locomotive-users] Problem with :belongs_to relation

Stephen Waits steve at waits.net
Mon Feb 26 18:23:01 GMT 2007


Samuel Linde wrote:
> 
> I've set up two models, Customer and Booking, with a has_many - 
> belongs_to relationship.

Ok yes, your schema all looks to be pretty basic, as you say.

> Here comes the weird part. The first time I call my list function in the 
> BookingsController, the page loads fine and shows the customer's name 
> correctly, but on subsequent browser reloads, I get a "NoMethodError", 
> stating "undefined method `customer' for #<Booking:0x32514c8>". I have 
> to kill my ruby process and restart the Rails app in Locomotive to make 
> it work again. Only to break on subsequent reloads, of course.
> 
> Does anyone know where I should start looking?

Booking#customer should be added to the class by the belongs_to call. 
Basically, no, I'm not sure what might cause this, but I'm wondering if 
you can try playing with some of this from script/console?

   script/console
   b = Booking.find(:first)
   b.customer?
   b.customer.nil?
   b.customer

Just wondering if that works, and then repeatedly, from script/console?

If so, then you might further try it with more than just :first.. i.e.

   Booking.find(:all).each { |b| puts b.customer.name if b.customer? }

If so, then it would seem that maybe something's going on with 
mongrel/lighttpd/webrick.

--Steve



More information about the Locomotive-users mailing list