2011-09-01 14 views
5

मैंने हाल ही में इस एप्लिकेशन को रेल 2.2.2 से 2.3.11 तक अपडेट किया है। अपग्रेड से पहले सब कुछ ठीक चल रहा था। अपग्रेड करने के बाद मैं निम्न त्रुटि हो रही है:स्वयं रेफरेंसियल त्रुटि नहीं बना सकता

ActiveRecord::HasAndBelongsToManyAssociationForeignKeyNeeded in InstrumentsController#arrow 
Cannot create self referential has_and_belongs_to_many association on 'Trait#traits'. :association_foreign_key cannot be the same as the :foreign_key. 

उपहार मॉडल में:

class Gift < ActiveRecord::Base 
    has_many :delegate_gifts 
    has_many :answers 

    belongs_to :feel_motive, :class_name => "Trait", :foreign_key => "feel_motive_id" 
    belongs_to :see_motive, :class_name => "Trait", :foreign_key => "see_motive_id" 
    belongs_to :incline_motive, :class_name => "Trait", :foreign_key => "incline_motive_id" 

    has_and_belongs_to_many :users 
    has_and_belongs_to_many :best_contributions 

    def traits 
    traits = [] 
    traits << feel_motive unless feel_motive.nil? 
    traits << see_motive unless see_motive.nil? 
    traits << incline_motive unless incline_motive.nil? 
    return traits 
    end 
end 

विशेषता मॉडल:

class Trait < Field 
    has_and_belongs_to_many :traits 
end 

क्यों 2.2.2 से 2.3.11 उपज के उन्नयन करता है यह गलती?

+0

"स्वयं नहीं बना सकता"। निश्चित रूप से लगता है कि किसी की तरह अस्तित्ववाद बहुत कम गंभीरता से ले रहा है। – Layke

+1

क्या आप अपने 'ट्राइट' मॉडल के कोड को शामिल करने के लिए अपना प्रश्न संपादित कर सकते हैं। –

+0

@ जॉन टोप्ली ओप्स ... पोस्ट मॉडल में पोस्ट किया गया। – Jay

उत्तर

13

has_and_belongs_to_many स्वयं को इंगित नहीं कर सकता (कम से कम आसान तरीके से नहीं)। यही कारण है कि आपके पास "स्वयं रेफरेंशियल" त्रुटि है। उन तालिका में

class User < ActiveRecord::Base 
    has_and_belongs_to_many :friends, 
    :class_name => "User", 
    :association_foreign_key => "friend_id", 
    :join_table => "friends_users" 
end 

ताकि आप की जरूरत अतिरिक्त क्षेत्र friend_id और नए क्षेत्रों के साथ तालिका friends_users में शामिल होने: user_id और friend_id

नोट: यदि आप वास्तव में तो यह आवर्तक संघ की जरूरत है आप कुछ इस तरह लिखने के लिए है और अधिक जानकारी जो आप वहां पा सकते हैं: http://railsforum.com/viewtopic.php?id=4237)

संबंधित मुद्दे