【发布时间】:2013-10-10 09:45:06
【问题描述】:
我有一个与 has_one 的多态关联,当我尝试通过关联创建时它给了我一个错误。
class User < ActiveRecord::Base
belongs_to :userable, polymorphic: true
end
class Student < ActiveRecord::Base
attr_accessible :gender, :description, :dob
has_one :user, :as => :userable
end
如果我尝试这样做:
s = Student.new
s.user.create
我得到并错误 Undefined method create for 'nil'
但是!如果我将关联更改为 has_many 用户,那么我现在可以执行上述相同的行。
谁能解释为什么会这样?谢谢!
【问题讨论】:
标签: ruby-on-rails polymorphic-associations has-one