Andrew
Andrew

Reputation: 240

Rails Association - User + Profile

New to Rails and I have read the association manuals and tried to find an answer to why my association is not working.

I chose to separate the Profile from the user for ease of association to different user types and functions - I will have 3-4 different models for each type of user.

I have:

User model using Devise Profile Model Called Temper Dashboard View

If I am trying to call the information from the Profile model on the dashboard for the current user the code below "should" work if I am reading the guides right.

ERROR IS:

enter code hereundefined method `temp_phone_n' for #<User:0x007fbb8c62e4c8>

I see that it IS picking up the current user and trying to associate it but to no avail. Can anyone tell me where I am going wrong.

Below:

user.rb

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

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me,
                    :first_name, :last_name, :profile_name, :user_id
  # attr_accessible :title, :body

    has_one :temper

  def full_name
    first_name + " " + last_name
  end      
end

Temper.rb

class Temper < ActiveRecord::Base
  attr_accessible :first_name, :last_name, :temp_about, :temp_avail, :temp_city, :temp_country, :temp_email, :temp_phone_d, :temp_phone_n, :temp_pors, :temp_skills, :temp_st_address_1, :temp_st_address_2, :user_id

  has_one :user

end

Dashboard/index.html.erb

<div class="span5">

    <ul>

    <li><h4>Name:</h4> <%= current_user.full_name %></li>
    <li><h4>Phone Number:</h4> <%= current_user.temp_phone_n %></li>
    <li><h4>Email:</h4>  <%= current_user.email %></li>
    <li><h4>City:</h4>  Toronto</li>
    <li><h4>Member Since:</h4>  <%= current_user.created_at.strftime("%B %d, %Y") %></li>

    </ul>
    </div>

Any assistance would be appreciated

Upvotes: 0

Views: 525

Answers (1)

emrahbasman
emrahbasman

Reputation: 2013

You can access it like that:

current_user.temper.temp_phone_n

And your relationship is wrong, you can't write has_one to both side of the relationship

User.rb

has_one :temper

Temper.rb

belongs_to :user

Upvotes: 1

Related Questions