2
我正在嘗試創建一個模型,該模型是其他用戶的父類。 這是我現在所擁有的:與其他用戶的用戶關係
型號:
class User < ActiveRecord::Base
.
.
.
has_one :guardian_of_child
end
class GuardianOfChild < ActiveRecord::Base
belongs_to :user, foreign_key: "guardian_id"
has_many :users, foreign_key: "child_id"
end
遷移:
class CreateGuardianOfChildren < ActiveRecord::Migration
def change
create_table :guardian_of_children do |t|
t.references :user, index: true
t.timestamps
end
end
end
當我嘗試訪問 'guardian_id' 或 'child_id'
<% @guardian_of_children.each do |guardian_of_child| %>
<tr>
<td><%= guardian_of_child.user.guardian_id %></td>
它給了我這個錯誤:
undefined method `guardian_id' for nil:NilClass
我在做什麼錯?有人能幫我嗎?
你能澄清你試圖建模的關係嗎?從你申報的協會中並不清楚。例如has_many關係應該在另一邊有一個屬性。此外,你會很好地調試什麼是零。例如leave guard_id並查看是否顯示任何用戶 – superuseroi 2014-09-24 20:36:22
您可以提供一個structure.sql或schema.rb嗎? – 2014-09-26 00:53:05