2013-09-24 32 views
12

Phải. Điều này đơn giản từ chối làm việc. Đã ở đây trong nhiều giờ.Rails 4 Không thể tìm thấy liên kết has_many, thông qua: lỗi mối quan hệ

mô hình album

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

tính năng mô hình

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

join_table1 mô hình

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 

tính năng schema

id | feature | created_at | updated_at 

Khi cào cơ sở dữ liệu kiểm tra, và chạy thử nghiệm hội nhập này:

012.
require 'test_helper' 

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

tôi nhận được

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

Tại sao điều này?

Trả lời

13

Bạn cần phải thêm has_many :album_features cả Album và tính năng mô hình (cho rằng bạn đổi tên model JoinTable1 để có ý nghĩa hơn AlbumFeature, và bảng tương ứng sẽ là album_features), như hiệp hội tài liệu tham khảo :through - Lỗi của bạn là chính xác về nó.

Hoặc bạn có thể sử dụng has_and_belongs_to_many để không cần phải xác định mô hình liên kết đặc biệt. Nhưng trong trường hợp đó, bạn phải đặt tên cho bàn của mình là albums_features.

+2

Tôi không nghĩ quy ước đặt tên của bảng nối quan trọng với mối quan hệ has_many, thông qua mối quan hệ. – Starkers

+0

Bạn không thể có 2 kiểu máy có tên Feature, đó là điều quan trọng. – biomancer

+0

Ngoài ra, tên của một mô hình activerecord phải tương ứng với tên bảng của nó. – biomancer

5

Chỉ cần xác định các mô hình như sau

mô hình album

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

tính năng mô hình

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

join_table1 mô hình

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

Tương tự như @mad_raz đã trả lời, nhưng tham gia bảng cần phải có singulars cho belongs_to, như thế này:

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

hiệp hội Hoàn thành hướng dẫn https://kolosek.com/rails-join-table/

1

xảy ra với tôi là tốt. làm cho nó hoạt động bằng cách thêm bảng kết nối như has_many cho cả hai mô hình. mô hình kết nối:: như thế này mô hình

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

cảnh báo:

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 

cố mô hình: 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 

di cư:

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! 
Các vấn đề liên quan