2016-10-15 7 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

いただきまし凶悪列の命名スキームについての詳細を読むことができますか? – max

+0

あなたは何をやっているのか、レガシーデータベースで作業しようとしているのか分かりませんか? – max

+0

'EUserProfle'クラスはどのように見えますか?また、あなたは2つの '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スキーマが適合する場合にのみ機能することを覚えておいてください。

あなたは団体here

+0

私はまだ前と同じエラーが発生しています –

+0

データベースを削除して移行を再実行しましたか?命名規則にもタイプがあるかもしれませんが、 – Chen

+0

でしたので、何が起こっているのかまだ分かりません。 –

関連する問題