2

I have a rails 4 app.

I have a profiles model and an organisation model.

The associations are:

profile.rb:

belongs_to :organisation 

organisation.rb

has_many :profiles 

Then in my show page in my profiles folder, I have:

<% unless @profile.organisation.title.empty? %> <%= @profile.organisation.title %> <% end %> 

When I try this, I get a no method error:

undefined method `title' for nil:NilClass 

I don't understand this error. If there is no title, then there is nothing to display.

Can anyone see what I've done wrong?

Taking Miles' suggestion below, I tried changing my profiles show page to:

<% unless Organisation.find(@profile.organisation_id).title.empty? %> <%= Organisation.find(@profile.organisation_id).title %> <% end %> 

But, I get this error:

Couldn't find Organisation with 'id'= 

The line starting with 'unless' is highlighted in the error message

2 Answers 2

1

The organization of your profile instance is nil so when @profile.organization.title call it actually call nil.title and it is throwing the error(undefined methodtitle' for nil:NilClass`)

You can use try to overcome in view level

<%= @profile.organisation.try(:title) %> 

Or you can give a validation check to overcome nil on title column on model level

on organization model

 class Organisation < ActiveRecord::Base validates :title, prescence: true end 
Sign up to request clarification or add additional context in comments.

3 Comments

This is the correct answer! Please try this @user2860931
Hi, I did try this. I get the same error as I got before I tried this. I think it has something to do with the point Miles made below.
Although Miles seems to have deleted his answer
0
<%= @profile.try(:organisation).try(:title) %> 

Docs.


The actual problem is that your @profile doesn't seem to have an associated organization.

This can be solved in a number of ways; I would personally use a validation to make sure that your organization is being populated with each profile creation:

#app/models/profile.rb class Profile < ActiveRecord::Base belongs_to :organisation validates :organisation, presence: true end 

Of course, this won't resolve your current issue.

To solve that, you need to make sure your organisation_id foreign key is populated with the correct reference to the Organization model:

$ rails c $ profile = Profile.find [[your id]] $ profile.update organisation_id: [[org_id]] $ exit 

If you get it working, a cool trick you can use is delegate:

#app/models/profile.rb class Profile < ActiveRecord::Base belongs_to :organisation validates :organisation, presence: true delegate :title, to: :organisation, prefix: true #-> @profile.organisation_title end 

Delegate, as the name suggests, takes your request and sends it through to a dependent class. It works especially well with belongs_to because it removes the need to reference the associative object in between your parent and dependent classes.

2 Comments

Hi Rich. I have tried the way you suggested. I often use that for fields the may not be populated, but it's not working this time. I'm not trying to force profile to have an organisation. I want it to show the organisation if there is one. There is a foreign key in my profile model for organisation_id.
I am going to try making this polymorphic and see if a different approach works out better for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.