Topic: Simplest has_many / belongs_to won't work

Hey there,I'm pulling out my hairs around now. I've followed the beginners tutorial and checked the associative records documentation. And as far as i can see, i'm doing nothing wrong. Could you guys please take a look and tell me what i'm doing wrong in this case?

I've got a route.rb(Badly choosen name, i know) and a routelocation.rb(This holds the route_id and lat/long for the route)They look like this:

Route:
[#<Route id: 1, user_id: 1, created_at: "2012-09-12 10:57:34", updated_at: "2012-09-12 11:01:39", routename: "banana">]

Routelocation:
[#<Routelocation id: 1, route_id: 1, latlng: "355.100, 366.343", created_at: "2012-09-12 10:56:59", updated_at: "2012-09-12 10:56:59">, #<Routelocation id: 2, route_id: 1, latlng: "375.100, 386.343", created_at: "2012-09-12 10:57:22", updated_at: "2012-09-12 10:57:22">]

Nonetheless i still get the following error when i try <%= Route.routelocations %> : undefined method `routelocations' for #<Array:0x4304f10>

This is how my RB's look:

route.rb

class Route < ActiveRecord::Base
  attr_accessible :user_id
  has_many :routelocations
end

routelocation.rb

class Routelocation < ActiveRecord::Base
  attr_accessible :latlng
  belongs_to :route
end

Thanks in advance!

Last edited by CaptainCarl (2012-09-12 07:23:12)

Re: Simplest has_many / belongs_to won't work

You say

<%= Route.routelocations %>

You need to understand the difference between a class and an object.

Route is a class not an object.
It's like a building.  You are asking the drawing of the buikding to tell you how many people are inside it when you actually need the building itself to tell you how many people are inside. Route is the model (The drawing or description of the table called routes) rather than an actual route record.

But the error you are receiving

 <%= Route.routelocations %> : undefined method `routelocations' for #<Array:0x4304f10>

further complicates things for you because Route is an array of routes for your application and has no link to your routes table.

Solution

Change the name of your table to a name that is not reserved
Use the instance not the class name in your code when you want to know routelocations

Also open up a rails console and type Route and Route.inspect and see what you get

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Simplest has_many / belongs_to won't work

Fixed: I had to loop through @trips first. In the loop i can call c.triplocations





Thanks for your reply!

I've done what you said, however i still can't get it to work. I think i'm missing the bigger picture or something, i don't know. I've changed the names(That was pretty stupid tongue) and using the instance again(Already did that in the first try but managed to not type it.)

This is what i have right now:

class Triplocation < ActiveRecord::Base
  attr_accessible :latlng, :trip_id
  belongs_to :trip
end

class Trip < ActiveRecord::Base
  attr_accessible :description, :title, :user_id
  has_many :triplocations
end

Where the table "trip" has the id(1) and triplocation had trip_id(2 records, both 1). However, i still get the undefined method error. So there must be something wrong still...

Last edited by CaptainCarl (2012-09-15 05:26:30)