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?
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
Bạn không thể có 2 kiểu máy có tên Feature, đó là điều quan trọng. – biomancer
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