Topic: Accessing attribute through association

Hi,

I'm learning Rails and wondering if there is a better way to do the following:

I have 2 tables, "Users" and "Profiles".
A user has one profile and a profile belongs to one user.

I am trying to access data in the profile table from the user controller.

@history = Profile.where(user_id: current_user.id) works,  but I'm wondering if there is another way?

Specifically, in a 1:1 relationship doesn't the attribute in the profile table become an attribute of the user table so something like

@history = current_user.profile.last_subject_id  is more like what I would have expected.

Last edited by dcastellano1 (2013-03-11 16:03:31)

Re: Accessing attribute through association

When you create a 1:1 relationship, to have it work as you expect you have to tell to your 'parent' model  (here your User model)  that it can access to its child attriibutes.
So in your User model

class User < ActiveRecord::Base

# here add ":profile_attributes" in the accessibles attributes
attr_accessible :email, :password, :password_confirmation, :profile_attributes
  has_secure_password

# relationship add "accepts_nested_attributes_for :profile" 
# I assume you have a "belongs_to :user" relationship in your Profile model

 has_one :profile, :dependent => :destroy
  accepts_nested_attributes_for :profile

Then in your views you can use

<%= user.profile.field_in_profile %>

<%= current_user.profile.myfield %>

And in your forms if you want to mix User fields and Profile fields you have to do (using simple_form)

<%= simple_form_for @user, :url => users_path, :html => {:class => 'form-horizontal'} do |f| %>
    <!-- error(s) message -->
    <% if @user.errors.any? %>
        <div class="error_explanation">
          <div class="alert alert-error">
            The form contains <%= pluralize(@user.errors.count, "error") %>.<br/>
            Please review the entries below :
            <ul>
              <% @user.errors.full_messages.each do |msg| %>
                  <ul>
                    <li><%= msg %></li>
                  </ul>
              <% end %>
            </ul>
          </div>
        </div>
    <% end %>
    <!-- PROFILE FIELDS (nested) -->
    <section class="profile-fields">
      <%= simple_fields_for :profile, @user.profile do |pf| %>
          <%= pf.input :first_name, :placeholder => 'your first name' %>
          <%= pf.input :last_name, :placeholder => 'your last name' %>
      <% end %>
    </section>
    

    <!-- USER FIELDS  in this case I need only the email field for User-->
    <section class="user-fields">
      <%= f.input :email, :placeholder => 'your email', :type => 'email' %>
    </section>
    
     [... submit button etc ]

<% end %>


Hope this helps you a bit
Cheers

Re: Accessing attribute through association

Thanks, that really helped.

By the way,  Is BBcode that what is used to put the code in a text box?  If so, how is it done??

Thanks again for the help.

Re: Accessing attribute through association

Glad if it helped you ^_^

Yep it's BBCode
You just have to put your code between [ code ] and [ / code ] tags (without the spaces of course !)
Cheers
Have swing