0
我跟着崗位http://techspry.com/ruby_and_rails/multiple-table-inheritance-in-rails-3/實現與鐵4的多表繼承。我有三種模式:用戶,申請人和導師。這裏是我的代碼:未知的屬性errror時,實現軌道多表繼承
class User < ActiveRecord::Base
belongs_to :student, :polymorphic => true
end
class Tutor < ActiveRecord::Base
acts_as_student
end
class Applicant < ActiveRecord::Base
acts_as_student
end
# in the /lib/student_module.rb
module Student
def acts_as_student
include InstanceMethods
has_one :user, :as => :student, :autosave => true, :dependent => :destroy
alias_method_chain :user, :build
user_attributes = User.content_columns.map(&:name) #<-- gives access to all columns of Business
# define the attribute accessor method
def student_attr_accessor(*attribute_array)
attribute_array.each do |att|
define_method(att) do
user.send(att)
end
define_method("#{att}=") do |val|
user.send("#{att}=",val)
end
end
end
student_attr_accessor *user_attributes #<- delegating the attributes
end
module InstanceMethods
def user_with_build
user_without_build || build_user
end
end
end
用戶表具有用戶名,電子郵件attributes.The導師表有名字,姓氏,介紹,節目,entry_year屬性。 在軌控制檯,我得到了
tutor = Tutor.new => #<Tutor id: nil, first_name: nil, last_name: nil, intro: nil, created_at: nil, updated_at: nil, entry_year: nil, program: nil>
tutor.username
=> ActiveRecord::UnknownAttributeError: unknown attribute: student_id
,我發現,student_attr_accessor方法是錯誤。我應該如何解決它?謝謝!