Mario
Mario

Reputation: 1253

Belongs_to not working properly

I have this problem: enter image description here

And I have this code:

user.rb:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable  

  def full_name 
    "lalalala"
  end
end

status.rb:

class Status < ActiveRecord::Base

    belongs_to :user
end

show.html.erb:

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @status.user.full_name %>
</p>

<p>
  <strong>Content:</strong>
  <%= @status.content %>
</p>

<%= link_to 'Edit', edit_status_path(@status) %> |
<%= link_to 'Back', statuses_path %>

What am I missing here? I can't access the full_name method, but I don't know why. I'm new at Ruby-on-Rails so it must be some simple detail that I'm not understanding. rails version: 4.1.5; ruby version: 2.1.2

Upvotes: 1

Views: 88

Answers (4)

Nitin
Nitin

Reputation: 7366

All the above answer can solve your view issue. But there are many issue you may face because of your current code.

Few things that I found that creating issue:

1st : You are getting @status.user to nil. That means your user_id column (That must be there) in status not setting properly. So set that first then use solution above to rescue you from error.

2nd : You don't specified any relation in your status model. You should have has_many :statuses or has_one :status in your user model.

3rd : To make sure you have user_id column properly populated in your status table. try create it like below:

if you have has_one relation :

@user_object.status.create(status_params)

if you have has_many relation :

@user_object.statuses.create(status_params)

Upvotes: 2

Richard Peck
Richard Peck

Reputation: 76784

Further to Marek's comment, you'll also want to look at using the .delegate method, to prevent the Law Of Demeter (having more than one "point" to call a method):

#app/models/status.rb
class Status < ActiveRecord::Base
   belongs_to :user
   delegate :full_name, to: :user, prefix: true
end

This will give you the ability to call:

@status.user_full_name

Validity

As Marek said, you'll likely not have any user object associated to your status.

To determine if this is the problem, you'll want to use a conditional statement to check if the user object actually exists before calling it:

<%= @status.user_full_name if @status.user.present? %>

This will give you the ability to call the associated user object is present

Upvotes: 3

shweta
shweta

Reputation: 8169

You do not have User associated with @status

<%= @status.user.try(:full_name) %>

The above will not raise error when there is no user associated.

Upvotes: 0

Marek Lipka
Marek Lipka

Reputation: 51171

You have all you need in your error message. Your Status record doesn't have associated User record, so @status.user returns nil.

Upvotes: 4

Related Questions