Bogdan Dinu
Bogdan Dinu

Reputation: 1

How to connect two models (through associations?) in Rails 3.2 and refer them in a view?

I'm trying to do a simple model association. I have an 'issues' table and a 'statuses' table. Every issue has a status. Table 'issues' has a 'status_id' column.

issue.rb

belongs_to :status

status.rb

has_many :issues

issues/index.html.erb

...
<% @issues.each do |issue| %>
...
<td><%= issue.status.title %></td>
...

I get the following error:

undefined method `title' for nil:NilClass

SOLUTION: Create all models associations BEFORE adding any records into the database (i.e. using scaffold).

Upvotes: 0

Views: 49

Answers (2)

Bogdan Dinu
Bogdan Dinu

Reputation: 1

SOLUTION: Create all models associations BEFORE adding any records into the database (i.e. using scaffold).

Upvotes: 0

user946611
user946611

Reputation:

undefined method 'title' for nil:NilClass means issue.status is nil.

You could do something like

<%= issue.status.title if issue.status %> 

Upvotes: 2

Related Questions