轨道关联问题 - id 显示,但不显示视图中的对象



我有一个简单的预订系统:

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

等。。。

预约表:

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

用于为特定课程创建记录的表单:

<%= 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 %>

这一切都按计划工作 - 用户可以进行预订并将许多用户添加到预订中。

当我想使用约会模型的user对象时,会出现问题:

 <%= appointment.user_id %>

上面的代码将 id 显示为整数,因此证明它已正确存储,但是

<%= appointment.user %> 

出来空白??

我不明白为什么因为我确定关系设置正确?一直在用这个拔头发。有什么想法吗?

我采用了您的代码并创建了一个示例应用程序。 我对其进行了测试,并认为问题是您需要在以下代码的输出中添加一个属性:

<%= appointment.user %> 

像这样:

<%= appointment.user.name %>

您当前正在执行的操作是它引用约会中的整个用户对象。 您还可以通过加载导轨控制台并执行以下操作来测试这一点:

appointment = Appointment.first
appointment.user

这将显示如下所示的用户对象:

>> 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">

一旦你有了它,你就可以更深入地挖掘你需要的东西。

让我知道这是否有效或您仍然遇到问题。 我假设 appointment.user 的输出在显示上下文中?

迈克·莱利

最新更新