The problem is that user is nil since you haven't assigned a value to it. You should use something like:
s.build_user(...)
or
s.create_user(...)
Вопрос
I've got a polymorphic association with a has_one and it gives me an error when trying to create through association.
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
If I try to do:
s = Student.new
s.user.create
I get and error Undefined method create for 'nil'
But! If i change the association to has_many users then I can now preform the same lines above.
Can anyone explain why this is happening? Thanks!
Решение
The problem is that user is nil since you haven't assigned a value to it. You should use something like:
s.build_user(...)
or
s.create_user(...)