2017-03-16 58 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