Should a AR object be able to see backwards?

I have two models, Stop and Station, they relate one station to many
stops.

Stop has_one :station
Station belongs_to :stop

So I can do @station.stops, but I cant do @stop.station, is this right?

For example in the view I have:

<% for @stop in @stops %>

<%= @stop.station.name %> <%= @stop.time.hour.to_s + ':' + @stop.time.min.to_s %> <% end %>

This line, <%= @stop.station.name %>, throws an error.

Many thanks, P.

On 3/7/06, Peter P. [email protected] wrote:

<% for @stop in @stops %>

<%= @stop.station.name %> <%= @stop.time.hour.to_s + ':' + @stop.time.min.to_s %> <% end %>

This line, <%= @stop.station.name %>, throws an error.

Many thanks, P.

You can if it has a related station. However, @station.stops
shouldn’t work because Station belongs_to :shop. @station.shops
implies that Shop belongs_to :station (The foreign key station_id is
in shops) and that Station has_many :shops.


Rick O.
http://techno-weenie.net

Thanks for the reply, in my table stops has station_id foreign key.
Thats stops, not shops :wink:

Each stop has the station_id fill out, so I should be able to get the
station from the stop, right?

@stop.station.name

or are my belongs_to / has_one wrong?

Rick O. wrote:

On 3/7/06, Peter P. [email protected] wrote:

<% for @stop in @stops %>

<%= @stop.station.name %> <%= @stop.time.hour.to_s + ':' + @stop.time.min.to_s %> <% end %>

This line, <%= @stop.station.name %>, throws an error.

Many thanks, P.

You can if it has a related station. However, @station.stops
shouldn’t work because Station belongs_to :shop. @station.shops
implies that Shop belongs_to :station (The foreign key station_id is
in shops) and that Station has_many :shops.


Rick O.
http://techno-weenie.net

On 3/7/06, Peter P. [email protected] wrote:

Thanks for the reply, in my table stops has station_id foreign key.
Thats stops, not shops :wink:

Each stop has the station_id fill out, so I should be able to get the
station from the stop, right?

@stop.station.name

or are my belongs_to / has_one wrong?

Since the foreign key is in stops, you do:

class Stop < AR::Base
belongs_to :station
end

@stop.station.name only works if station is set. If it’s not you get
a nil error trying to call #name on NilClass.

More info can be found in the docs:
http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html


Rick O.
http://techno-weenie.net

Thanks Rick, I had the assosiations the wrong way round!
It should have been:

Stop belongs_to :station
Station has_many :stops

not

Stop has_one :station
Station belongs_to :stop

Many thanks!

Rick O. wrote:

On 3/7/06, Peter P. [email protected] wrote:

Thanks for the reply, in my table stops has station_id foreign key.
Thats stops, not shops :wink:

Each stop has the station_id fill out, so I should be able to get the
station from the stop, right?

@stop.station.name

or are my belongs_to / has_one wrong?

Since the foreign key is in stops, you do:

class Stop < AR::Base
belongs_to :station
end

@stop.station.name only works if station is set. If it’s not you get
a nil error trying to call #name on NilClass.

More info can be found in the docs:
http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html


Rick O.
http://techno-weenie.net