I'm getting the following error. I know the reason is that I'm calling a function on a related record that hasn't been related yet, but didn't know the best way to fix this.
Code causing the error:
<%= link_to member.names.last.fullname, polymorphic_path([member, Name]) %>
In this example Name has a belongs_to relationship with Member and Member has a has_many relationship with Name. It happens to be a polymorphic relationship, but I don't think that matters for this error.
When the above code is called and there are no related Names the error is:
undefined method `fullname' for nil:NilClass
I'm sure there is something very basic I'm missing here that should prevent this from happenning. Thanks in advance for any help that you all can offer!
Mark
More Code:
class Name < ActiveRecord::Base
belongs_to :person, polymorphic: true
attr_accessible :dob, :dod, :first, :gender, :last, :mi, :prefix, :relation, :suffix
def fullname
first + last
end
end
class Member < ActiveRecord::Base
has_many :names, as: :person
attr_accessible :active, :deleted
end