2013-09-24 16 views
12

Dobrze. To po prostu odmawia pracy. Byłem w tym przez wiele godzin.Szyny 4 Nie można znaleźć powiązania has_many, przez: błąd relacji

modelu album

class Album < ActiveRecord::Base 
    has_many :features, through: :join_table1 
end 

cechy modelu

class Feature < ActiveRecord::Base 
    has_many :albums, through: :join_table1 
end 

join_table1 modelu

class JoinTable1 < ActiveRecord::Base 
    belongs_to :features 
    belongs_to :albums 
end 

join_table1 schematu

album_id | feature_id 

albumy schematu

id | title | release_date | genre | artist_id | created_at | updated_at | price | image_path 

cechy schematu

id | feature | created_at | updated_at 

Upon grabienie bazę testową, a uruchomienie tego testu integracyjnego:

require 'test_helper' 

class DataFlowTest < ActionDispatch::IntegrationTest 
    test "create new user" do 
    album = albums(:one) 
    feature = features(:one) 
    album.features 
    end 
end 

uzyskać

ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :join_table1 in model Album

Dlaczego tak jest?

Odpowiedz

13

Trzeba dodać has_many :album_features zarówno do albumu i wyposażone modele (biorąc pod uwagę, że zmiana nazwy modelu JoinTable1 do bardziej sensowne AlbumFeature i odpowiedni stół byłby album_features), jako stowarzyszenie :through odnośników - Twój błąd jest dokładnie o tym.

Lub można użyć has_and_belongs_to_many, więc nie będzie potrzeby definiowania specjalnego modelu łącza. Ale w takim przypadku musisz nazwać swój stół albums_features.

+2

Nie sądzę, że konwencja nazewnictwa tabeli złączeń ma znaczenie w związku. – Starkers

+0

Nie możesz mieć 2 modeli o nazwie Feature, to się liczy. – biomancer

+0

Ponadto nazwa modelu aktywnego rekordu powinna odpowiadać nazwie tabeli. – biomancer

5

Wystarczy zdefiniować modele jak postępować

modelu album

class Album < ActiveRecord::Base 
    has_many :join_table1 
    has_many :features, through: :join_table1 
end 

cechy modelu

class Feature < ActiveRecord::Base 
    has_many :join_table1 
    has_many :albums, through: :join_table1 
end 

join_table1 modelu

class JoinTable1 < ActiveRecord::Base 
    belongs_to :features 
    belongs_to :albums 
end 
0

Podobnie jak @mad_raz odpowiedział, ale przyłączyć tabela musi mieć singulars dla belongs_to, tak:

class JoinTable1 < ActiveRecord::Base 
    belongs_to :feature 
    belongs_to :album 
end 

Kompletny stowarzyszenia poradnik https://kolosek.com/rails-join-table/

1

się do mnie. sprawił, że zadziałał, dodając tabelę dołączeń jako has_many do obu modeli. Model gra: tak

module Alerts 
    class AlertIncidentConnection < ActiveRecord::Base 
    belongs_to :incident 
    belongs_to :alert 
    end 
end 

alert modelu: model

module Alerts 
    class Alert < ActiveRecord::Base 
    has_many :alert_incident_connections, class_name: 'Alerts::AlertIncidentConnection' 
    has_many :incidents, through: :alert_incident_connections,class_name: 'Alerts::Incident', dependent: :destroy 
    end 
end 

incydent: file

module Alerts 
    class Incident < ActiveRecord::Base  
    has_many :alert_incident_connections, class_name: 'Alerts::AlertIncidentConnection' 
    has_many :alerts, through: :alert_incident_connections,class_name: 'Alerts::Alert' ,dependent: :destroy 
    end 
end 

migracja:

class CreateTableAlertIncidentConnections < ActiveRecord::Migration 
    def change 
    create_table :alert_incident_connections do |t| 
     t.references :alert, null: false, index: true 
     t.references :incident, null: false, index: true 
     t.timestamps 
    end 
    end 
end 

USA ge:

alert.incidents << incident 
alert.save! 
Powiązane problemy