2013-09-24 14 views
12

Right. Questo semplicemente si rifiuta di lavorare. Sono stato a questo per ore.Rails 4 Impossibile trovare l'associazione has_many, tramite: relationship error

album modello

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

caratteristiche modello

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

join_table1 modello

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

schema join_table1

album_id | feature_id 

album schema

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

caratteristiche dello schema

id | feature | created_at | updated_at 

Su rastrellando il database di test, e l'esecuzione di questo test di integrazione:

require 'test_helper' 

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

ottengo

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

Perché è questo?

risposta

13

è necessario aggiungere has_many :album_features sia ai modelli di album e Feature (dato che si rinomina il modello JoinTable1 al più significativo AlbumFeature, e tabella corrispondente sarebbe album_features), come associazione :through referenze - Il tuo errore è esattamente su di esso.

Oppure è possibile utilizzare has_and_belongs_to_many in modo che non sia necessario definire un modello di collegamento speciale. Ma in tal caso è necessario nominare il proprio tavolo albums_features.

+2

Non penso che la convenzione di denominazione della tabella di join abbia una relazione has_many, through. – Starkers

+0

Non è possibile avere 2 modelli con il nome Feature, questo è ciò che conta. – biomancer

+0

Inoltre, il nome di un modello activerecord deve corrispondere al nome della tabella. – biomancer

5

Basta definire i modelli da seguire

album modello

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

caratteristiche modello

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

join_table1 modello

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

Allo stesso modo come @mad_raz rispose, ma unire tavolo ha bisogno di avere singolari per belongs_to, in questo modo:

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

associazioni tutorial completo https://kolosek.com/rails-join-table/

1

accaduto a me pure. ha funzionato aggiungendo la tabella di join come has_many a entrambi i modelli. il modello collegamento:: come questo modello

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

avviso: modello

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 

incidente: file di

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 

migrazione:

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 

Stati Uniti d'America ge:

alert.incidents << incident 
alert.save! 
Problemi correlati