2013-09-24 6 views
12

दाएं। यह बस काम करने से इंकार कर देता है। घंटों के लिए इस पर किया गया।रेल 4 एसोसिएशन नहीं मिला है, के माध्यम से: रिश्ते त्रुटि

एलबम मॉडल

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

सुविधाओं मॉडल

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

join_table1 मॉडल

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

join_table1 स्कीमा

album_id | feature_id 

एलबम स्कीमा

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

सुविधाओं स्कीमा

id | feature | created_at | updated_at 

परीक्षण डेटाबेस बटोर, और चल रहा है इस एकीकरण परीक्षण पर:

012,
require 'test_helper' 

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

मैं

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

ऐसा क्यों है मिल सकता है?

उत्तर

13

आप एलबम और फ़ीचर मॉडल के लिए has_many :album_features दोनों को जोड़ने के लिए (है कि आप JoinTable1 मॉडल के लिए और अधिक सार्थक AlbumFeature नाम बदलें, और इसी तालिका album_features होगा दी गयी है), के रूप में :through संदर्भ संघ की जरूरत है - अपने त्रुटि वास्तव में इसके बारे में है।

या आप has_and_belongs_to_many का उपयोग कर सकते हैं, इसलिए विशेष लिंक मॉडल को परिभाषित करने की आवश्यकता नहीं होगी। लेकिन उस स्थिति में आपको अपनी तालिका albums_features नाम देनी होगी।

+2

मुझे नहीं लगता कि संबंध तालिका के नामकरण सम्मेलन में हैसनी के साथ संबंध है। – Starkers

+0

आपके पास नाम फ़ीचर के साथ 2 मॉडल नहीं हो सकते हैं, यही मायने रखता है। – biomancer

+0

इसके अलावा, एक सक्रियरेकॉर्ड मॉडल का नाम इसके तालिका के नाम से मेल खाना चाहिए। – biomancer

5

बस मॉडल को परिभाषित का पालन के रूप में

एलबम मॉडल

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

सुविधाओं मॉडल

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

join_table1 मॉडल

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

@mad_raz इसी तरह उत्तर दिया, लेकिन में शामिल होने की मेज, belongs_to के लिए singulars के लिए इस तरह की जरूरत है:

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

पूरा संघों ट्यूटोरियल https://kolosek.com/rails-join-table/

1

रूप में अच्छी तरह मुझे क्या हुआ। यह दोनों मॉडलों के लिए has_many के रूप में तालिका में शामिल होने जोड़कर काम कर दिया। इस तरह: कनेक्शन मॉडल:

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

चेतावनी मॉडल:

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 

घटना मॉडल:

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 

प्रवास फ़ाइल:

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 

यूएसए जीई:

alert.incidents << incident 
alert.save! 
संबंधित मुद्दे