Rails, CamalCase Model Name retrieving related record in view?
I have two mod开发者_如何学Goels Location and LocationType.
class LocationType < ActiveRecord::Base
has_many :locations
end
class Location < ActiveRecord::Base
belongs_to :location_type
end
In my Location model I have the foreign key: location_type_id
In my view I try and call:
@location.location_type.name
but I get an error:
undefined method `name' for nil:NilClass
How do I get the view to display the location_type related record?
Check the location_type_id
on your Location
object. If there is no LocationType
object with that id, then @location.location_type
will return nil.
精彩评论