2017-03-16 56 views
0

我试图列出我的应用程序中的教师记录。我有一个嵌套的Polymorphic Contact对象(这是与学生共享的),当我加载教师时不加载。
这里有对象:Rails嵌套对象返回零

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

但我得到以下错误a:

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: "[email protected]", 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"]] 

谢谢!

+0

我可能与此有关 ''' belongs_to:teacher,foreign_type:'Teacher',foreign_key:'contactable_id',required:tru e ''' 它是多态的,所以你不需要指定关联两次。也许它与关联解析器混淆。 –

回答

1

如果看到有两条记录Teacher但只有一条Contact

问题是您遍历Teacher.all<%= teacher.contact.username %>通过使用:try

teacher.contact.try(:username) 
teacher.contact.try(:email) 
# and so on.. 
引发错误的记录 #<Teacher id: 1,...因为你没有礼物 contactable_type = "Teacher" AND contactable_id = 2

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: "[email protected]", contactable_type: "Teacher", contactable_id: 2, created_at: "2017-03-14 18:04:52", updated_at: "2017-03-14 18:04:52">]> 

您可以修复它接触

+0

啊!!!!谢谢!我整个星期都在抨击这件事,只是没有点击。 (我在.NET和React中开发了好几年,只是拿起Rails,所以感觉有点笨拙。) – Erick