2016-10-15 5 views
0

나는 매우 간단하게하려고 노력하고 있습니다.간단한 has_many가 작동하지 않습니다

프로필을 여러 리뷰와 연결하고 싶습니다. 지금 리뷰를 저장하려고하면 콘솔에서 BEGIN을 출력 한 다음 즉시 ROLLBACK을 실행하여 Euserprofile이 존재해야한다고 말합니다.

class EUserProfile < ApplicationRecord 
    has_many :EUserRevs 
    has_and_belongs_to_many :EUserProfiles, 
          class_name: "EUserProfile", 
          join_table: :e_user_friends, 
          foreign_key: :E_USER_FRIEND_TO, 
          association_foreign_key: :E_USER_FRIEND_FROM 

    has_and_belongs_to_many :EUserProfiles, 
          class_name: "EUserProfile", 
          join_table: :e_user_follows, 
          foreign_key: :E_USER_FOLR, 
          association_foreign_key: :E_USER_FOLG 
end 


class CreateEUserRevs < ActiveRecord::Migration[5.0] 
    def up 
    create_table :e_user_revs, {:id => false} do |t| 
     t.string "E_USER_REV" 
     t.string "E_USER_REVING", :null => false 
     t.integer "E_USER_SCORE", :null => false 
     t.string "E_USER_COMMENT", :null => false 

     t.timestamps 
    end 

    end 

    def down 

    drop_table :e_user_revs 
    end 
end 

class CreateEUserProfiles < ActiveRecord::Migration[5.0] 
    def up 
    create_table :e_user_profiles do |t| 
     t.string "E_USER_PROFILE_USERNAME", :null => false 
     t.string "E_USER_PROFILE_FIRST_NAME", :null => false 
     t.string "E_USER_PROFILE_LOC" 
     t.string "E_USER_PROFILE_BIO" 
     t.string "E_USER_PROFILE_INSTR", :null => false 
     t.string "E_USER_PROFILE_GENR", :null => false 

     t.timestamps 
    end 
    end 
    def down 
    drop_table :e_user_profiles 
    end 
end 

class EUserRev < ApplicationRecord 
    belongs_to :EUserProfile 
end 
+0

뭐죠 here에 대한 자세한 내용을보실 수 있습니다? – max

+0

레거시 데이터베이스로 어떤 작업을하고 있거나 현재 작업하고 있는지 전혀 알지 못합니까? – max

+0

'EUserProfle' 클래스는 어떻게 생겼습니까? 또한, 두 개의'has_and_belongs_to_many : EUserProfiles'를 가질 수 없습니다. 어떻게 사용 중인지 구별 할 수 있습니까? 그것은'has_and_belongs_to_many : friends ... '와'has_and_belongs_to_many : followers ...'이어야합니다. 그래서'@ my_profile.friends'와'@ my_profile.followers'를 참조 할 수 있습니다.'@ my_profile.EUserProfiles' 그리고 당신이 돌아 오는 것을 알지 못할 것입니다. 또한, 천국을 위해서 수도 편지를 제거하십시오. 클래스 이름을 포함하여 상수에만 사용하십시오. – SteveTurczyn

답변

0

글쎄, 이것은 단순한 일대 다 관계입니다.

당신은 CreateEUserRevs 마이그레이션 파일에 다음 줄을 추가해야합니다

t.belongs_to : eu_user_profile, index: true 

EUserRevs 스키마에 대한 e_user_profile_id 필드를 추가 할 것이다. 그런 다음 has_many (EUserProfile 모델에서) 및 belongs_to (EUserRev 모델에서)를 사용할 수 있습니다.

ActiveRecord가 제공하는이 헬퍼는 DB 스키마가 적합 할 때만 작동합니다.

당신은 협회 극악한 열 명명 체계와

+0

이전과 같은 오류가 발생했습니다 –

+0

db를 삭제하고 마이그레이션을 다시 실행 했습니까? 명명 규칙에도 형식이있을 수 있습니다. – Chen

+0

, 그래서 아직도 무슨 일이 일어나고 있는지 확신 할 수 없습니다. –

관련 문제