Question

I have a simple booking system:

class User < ActiveRecord::Base
   has_many :appointments
   has_many :bookings
end

class Appointment < ActiveRecord::Base
   belongs_to :booking
   belongs_to :user
end

class Booking < ActiveRecord::Base
   belongs_to :course
   belongs_to :user
end

etc...

Appointments table:

add_index "appointments", ["booking_id"], name: "index_appointments_on_booking_id"
add_index "appointments", ["user_id"], name: "index_appointments_on_user_id"

create_table "appointments", force: true do |t|
  t.integer  "booking_id"
  t.integer  "user_id"
  t.boolean  "confirmed"
  t.boolean  "attended"
  t.datetime "created_at"
  t.datetime "updated_at"
end

the form to create the records for a particular course:

<%= form_for([@course, @booking]) do |f| %>
  <% if @booking.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@booking.errors.count, "error") %> prohibited this booking from being saved:</h2>

      <ul>
      <% @booking.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.hidden_field :course_id, :value => @course.id %>

  <div class="field">
    <%= f.label "Name" %><br>
    <%= f.text_field :title %>
  </div>

  <div class="field">
    <%= f.label "Description" %><br>
    <%= f.text_area :description %>
  </div>

  <h3>When?</h3>

  <div class="field">
    <%= f.date_select :start_date %>
  </div>
  <div class="field">
    <%= label_tag "until" %>
    <%= check_box_tag(:end_date) %>
  </div>
  <div class="field" id="end_date_field", style="display:none">
    <%= f.date_select :end_date %>
  </div>
  <div class="field">
    <%= f.label "starts at" %><br>
    <%= f.time_select :start_time %>
  </div>
  <div class="field">
    <%= f.label "ends at" %><br>
    <%= f.time_select :end_time %>
  </div>
  <div class="field">
    <%= label_tag "repeats" %>
    <%= check_box_tag(:recurring) %>
  </div>

  <div class="field" id="recurring_fields" style="display:none">
      <%= render 'recur'  %>
  </div>


<h3>Students</h3>
  <div id="students">
    <div class="items">
      <%= f.nested_fields_for :appointments do |s| %>
        <fieldset class="item">

          <%= s.collection_select(:user_id, @students, :id, :students_name, :prompt => false) %>

          <a href="#" class="remove">remove</a>

          <%= s.hidden_field :_destroy %>   
        </fieldset>
      <% end %>   
    </div>

    <a href="#" class="add">Add Student</a>
  </div>

  <br>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

This all works as planned - the user can make a booking and add many users to a booking.

The problem arises when I want to use the user object of the appointments model:

 <%= appointment.user_id %>

the code above shows the id as an integer so proves it was stored correctly BUT

<%= appointment.user %> 

comes out blank??

I can't understand why as I'm sure the relationships are set up correctly? Been pulling my hair out with this. Any ideas?

Was it helpful?

Solution

I took your code and created a sample application. I tested it out and think the issue is that you need to add an attribute to your output of the following code:

<%= appointment.user %> 

Something like:

<%= appointment.user.name %>

What you are doing currently is that it is referencing the entire user object from the appointment. You can also test this out by loading rails console and doing the following:

appointment = Appointment.first
appointment.user

This will show a User object like the following:

>> appointment.user
User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY    "users"."id" ASC LIMIT 1  [["id", 1]]
=> #<User id: 1, name: "test", created_at: "2014-02-28 16:12:15", updated_at: "2014-02-28 16:12:15">

Once you have that you can dig deeper to what you need.

Let me know if this works or you still are having trouble. I am assuming that the output of the appointment.user is in a show context?

Mike Riley

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top