2013-05-06 3 views
0

레일 4를 사용하여 다중 테이블 상속을 구현하기 위해 http://techspry.com/ruby_and_rails/multiple-table-inheritance-in-rails-3/ 게시물을 따라갔습니다. 사용자, 신청자 및 교사의 세 가지 모델이 있습니다.rails 다중 테이블 상속을 구현할 때 알 수없는 특성 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 교사 테이블이 FIRST_NAME, LAST_NAME, 소개, 프로그램, 속성 entry_year : 여기 내 코드입니다. 는 레일 콘솔에서, 나는 오류가 student_attr_accessor 방법에서 있었다 발견을

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 

을 얻었다. 어떻게 수정해야합니까? 감사!

답변

0

사용자 모델에서 외래 키 열과 형식 열을 선언하는 것을 잊어 버렸습니다. 이 문제를 해결하려면 다음과 같이 이전을 실행하십시오.

def change 
    add_column :users, :student_id,:integer 
    add_column :users, :student_type,:string 
    end