Forum: Ruby on Rails should a AR object be able to see backwards?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
285de58bd2ae7cac5f565682b74ef8e3?d=identicon&s=25 Peter Piper (Guest)
on 2006-03-07 15:32
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 %>
  <tr>
    <td><%= @stop.station.name %></td>
    <td><%= @stop.time.hour.to_s + ':' + @stop.time.min.to_s %></td>
  </tr>
<% end %>

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


Many thanks, P.
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-03-07 15:37
(Received via mailing list)
On 3/7/06, Peter Piper <pp@nispam.com> wrote:
> <% for @stop in @stops %>
>   <tr>
>     <td><%= @stop.station.name %></td>
>     <td><%= @stop.time.hour.to_s + ':' + @stop.time.min.to_s %></td>
>   </tr>
> <% 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 Olson
http://techno-weenie.net
285de58bd2ae7cac5f565682b74ef8e3?d=identicon&s=25 Peter Piper (Guest)
on 2006-03-07 16:37
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 Olson wrote:
> On 3/7/06, Peter Piper <pp@nispam.com> wrote:
>> <% for @stop in @stops %>
>>   <tr>
>>     <td><%= @stop.station.name %></td>
>>     <td><%= @stop.time.hour.to_s + ':' + @stop.time.min.to_s %></td>
>>   </tr>
>> <% 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 Olson
> http://techno-weenie.net
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-03-07 16:51
(Received via mailing list)
On 3/7/06, Peter Piper <pp@nispam.com> 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/...

--
Rick Olson
http://techno-weenie.net
285de58bd2ae7cac5f565682b74ef8e3?d=identicon&s=25 Peter Piper (Guest)
on 2006-03-07 18:08
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 Olson wrote:
> On 3/7/06, Peter Piper <pp@nispam.com> 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/...
>
> --
> Rick Olson
> http://techno-weenie.net
This topic is locked and can not be replied to.