Rails嵌套对象返回nil

时间:2017-03-16 16:58:55

标签: ruby-on-rails

我正在尝试在我的应用中列出教师记录。我有一个嵌套的Polymorphic Contact对象(这是与学生共享的),当我加载教师时,它没有加载 以下是对象:

class Teacher < ApplicationRecord
    has_one :contact, as: :contactable
    has_many :groups
    has_many :students, through: :groups
    validates :instrument, presence: true
    accepts_nested_attributes_for :contact, allow_destroy: true
end

class Contact < ApplicationRecord
    belongs_to :contactable, polymorphic: true
    belongs_to :teacher, foreign_type: 'Teacher', foreign_key: 'contactable_id', required: true

    validates :first_name, presence: true
    validates :last_name, presence: true
    validates :email, presence: true   
end

我在索引方法中加载所有教师:

def index
    @teachers = Teacher.all.includes(:contact)      
end

然后列出来:

 <% @teachers.each do |teacher| %>
      <tr>
          <td>
            <%= teacher.contact.username %>
          </td>
          <td>
            <%= teacher.contact.email %>
          </td>
          <td>
            <%= teacher.contact.first_name %>
          </td>
          <td>
            <%= teacher.contact.last_name %>
          </td>
          <td>
            <%= teacher.instrument %>
          </td>
          <td>
            <%= link_to 'Show', teacher_path(teacher) %>
          </td>
          <td>
            <%= link_to 'Edit', edit_teacher_path(teacher) %>
          </td>
          <td>
            <%= link_to 'Destroy', teacher_path(teacher),
              method: :delete,
              data: { confirm: 'Are you sure?' } %>
          </td>
      </tr>
  <% end %>

但是我收到以下错误:

undefined method `username' for nil:NilClass
 <tr>
          <td>
            <%= teacher.contact.username %>

基本上,联系人是空的。我已经尝试了各种方法,我可以想到为每个老师加载它但没有任何作用。记录在数据库中并且是正确的:

Teacher Load (1.2ms)  SELECT "teachers".* FROM "teachers"
 => #<ActiveRecord::Relation [#<Teacher id: 1, instrument: "Piano", created_at: "2017-03-14 18:04:30", updated_at: "2017-03-14 18:04:30">, #<Teacher id: 2, instrument: "Pia
no", created_at: "2017-03-14 18:04:52", updated_at: "2017-03-14 18:04:52">]>
2.4.0 :005 > Contact.all
  Contact Load (0.2ms)  SELECT "contacts".* FROM "contacts"
 => #<ActiveRecord::Relation [#<Contact id: 1, username: "ericklind", first_name: "Erick", last_name: "Lind", email: "erick@ericklind.com", contactable_type: "Teacher", con
tactable_id: 2, created_at: "2017-03-14 18:04:52", updated_at: "2017-03-14 18:04:52">]>

此外,查询正在选择记录,因此它们应该在那里:

Started GET "/teachers" for ::1 at 2017-03-16 09:46:57 -0700
Processing by TeachersController#index as HTML
  Teacher Load (0.1ms)  SELECT "teachers".* FROM "teachers"
  Contact Load (0.1ms)  SELECT "contacts".* FROM "contacts" WHERE "contacts"."contactable_type" = ? AND "contacts"."contactable_id" IN (1, 2)  [["contactable_type", "Teacher"]]

谢谢!

1 个答案:

答案 0 :(得分:1)

如果您发现有两条Teacher但只有一条Contact的记录。

问题是您正在循环Teacher.all<%= teacher.contact.username %>因记录#<Teacher id: 1,... <而未提供联系人而导致记录错误contactable_type = "Teacher" AND contactable_id = 2 < / p>

  Teacher Load (1.2ms)  SELECT "teachers".* FROM "teachers"
 => #<ActiveRecord::Relation [#<Teacher id: 1, instrument: "Piano", created_at: "2017-03-14 18:04:30", updated_at: "2017-03-14 18:04:30">,  #<Teacher id: 2, instrument: "Piano", created_at: "2017-03-14 18:04:52", updated_at: "2017-03-14 18:04:52">]>

2.4.0 :005 > Contact.all
  Contact Load (0.2ms)  SELECT "contacts".* FROM "contacts"
 => #<ActiveRecord::Relation [#<Contact id: 1, username: "ericklind", first_name: "Erick", last_name: "Lind", email: "erick@ericklind.com", contactable_type: "Teacher", contactable_id: 2, created_at: "2017-03-14 18:04:52", updated_at: "2017-03-14 18:04:52">]>

您可以使用:try

进行修复
teacher.contact.try(:username) 
teacher.contact.try(:email)
# and so on..