2011-09-01 26 views
5

Niedawno zaktualizowałem tę aplikację z szyn 2.2.2 do 2.3.11. Wszystko przebiegało dobrze przed aktualizacją. Po uaktualnieniu otrzymuję następujący błąd:Nie można utworzyć błędu samooceny

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. 

W modelu Gift: modelu

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 

cecha:

class Trait < Field 
    has_and_belongs_to_many :traits 
end 

Dlaczego aktualizacji z 2.2.2 do 2.3.11 produktów ten błąd?

+0

"Nie można utworzyć siebie". Zdaje się, że ktoś zbyt poważnie traktuje egzystencjalizm. – Layke

+1

Czy możesz edytować swoje pytanie, aby dołączyć kod dla swojego modelu 'Trait'. –

+0

@JohnTopley oops ... model cech dodany do postu. – Jay

Odpowiedz

13

has_and_belongs_to_many nie może wskazywać na siebie (przynajmniej nie w łatwy sposób). Właśnie dlatego masz błąd "samo referencyjny". Jeśli naprawdę potrzebujesz tej nawracające stowarzyszenie potem trzeba napisać coś takiego:

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

więc potrzebne są dodatkowe pola friend_id w tabeli użytkowników i nowe przyłączenia friends_users tabeli z polami: user_id i friend_id

Uwaga: więcej informacje, które można tam znaleźć: http://railsforum.com/viewtopic.php?id=4237)

Powiązane problemy