# Should a AR object be able to see backwards?

**URL:** <https://www.ruby-forum.com/t/should-a-ar-object-be-able-to-see-backwards/54515>\
**Category:** Rails\
**Created:** [March 7, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/should-a-ar-object-be-able-to-see-backwards/54515 "2006-03-07T15:32:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@peterk](https://www.ruby-forum.com/u/peterk)\
**Post date:** [March 7, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/should-a-ar-object-be-able-to-see-backwards/54515/1 "2006-03-07T15:32:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![peterk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@peterk](https://www.ruby-forum.com/u/peterk)\
**Post date:** [March 7, 2006, 3:37pm UTC](https://www.ruby-forum.com/t/should-a-ar-object-be-able-to-see-backwards/54515/2 "2006-03-07T15:37:44Z")

</div>

On 3/7/06, Peter P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![peterk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@peterk](https://www.ruby-forum.com/u/peterk)\
**Post date:** [March 7, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/should-a-ar-object-be-able-to-see-backwards/54515/3 "2006-03-07T16:37:33Z")

</div>

Thanks for the reply, in my table stops has station\_id foreign key.  
Thats stops, not shops 😉

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![peterk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@peterk](https://www.ruby-forum.com/u/peterk)\
**Post date:** [March 7, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/should-a-ar-object-be-able-to-see-backwards/54515/4 "2006-03-07T16:51:35Z")

</div>

On 3/7/06, Peter P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the reply, in my table stops has station\_id foreign key.  
> Thats stops, not shops 😉
> 
> 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](http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)

–  
Rick O.  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![peterk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@peterk](https://www.ruby-forum.com/u/peterk)\
**Post date:** [March 7, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/should-a-ar-object-be-able-to-see-backwards/54515/5 "2006-03-07T18:08:48Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Thanks for the reply, in my table stops has station\_id foreign key.  
> > Thats stops, not shops 😉
> > 
> > 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](http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)
> 
> –  
> Rick O.  
> [http://techno-weenie.net](http://techno-weenie.net)
