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. -- Posted via http://www.ruby-forum.com/.
On 3/7/06, Peter Piper <pp@nispam.com> wrote:> 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.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
Peter Piper
2006-Mar-07 15:37 UTC
[Rails] Re: should a AR object be able to see backwards?
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-- Posted via http://www.ruby-forum.com/.
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/Associations/ClassMethods.html -- Rick Olson http://techno-weenie.net
Peter Piper
2006-Mar-07 17:08 UTC
[Rails] Re: Re: should a AR object be able to see backwards?
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/Associations/ClassMethods.html > > -- > Rick Olson > http://techno-weenie.net-- Posted via http://www.ruby-forum.com/.