【问题标题】:Rails 4 Could not find the association has_many, through: relationship errorRails 4找不到关联has_many,通过:关系错误
【发布时间】:2013-09-29 11:50:46
【问题描述】:

没错。这只是拒绝工作。在这里呆了几个小时。

专辑模型

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

在获取测试数据库并运行此集成测试时:

require 'test_helper'

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

我明白了

ActiveRecord::HasManyThroughAssociationNotFoundError:在模型相册中找不到关联:join_table1

这是为什么?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 has-many-through


    【解决方案1】:

    您需要将 has_many :album_features 同时添加到 Album 和 Feature 模型(假设您将 JoinTable1 模型重命名为更有意义的 AlbumFeature,并且相应的表将是 album_features),因为 :through 引用关联 - 您的错误正是关于它。

    或者您可以使用has_and_belongs_to_many,这样就不需要定义特殊的链接模型。但在这种情况下,您必须将表命名为 albums_features

    【讨论】:

    • 我认为连接表的命名约定与 has_many 关系无关。
    • 你不能有 2 个名称为 Feature 的模型,这很重要。
    • 另外,一个activerecord模型的名字应该和它的表名相对应。
    • 如果命名约定很重要,您会将其命名为 album_features 还是 features_album ?正如@starkers 提到的那样。我遇到了同样的问题,除了 through 之外,将 has_many 同时添加到 Album 和 Features 是答案的唯一相关部分。
    • @msanjay 是的,你是对的,命名与 has_many 无关:通过(尽管与 has_and_belongs_to_many 无关,afaik 字母顺序用于决定表名中的第一个),这是针对第一个版本的问题,其中有 2 个错误。问题已更新,所以我也会更新答案,谢谢。
    【解决方案2】:

    只需定义模型如下

    专辑模型

    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
    

    【讨论】:

      【解决方案3】:

      类似于@mad_raz 的回答,但连接表需要为 belongs_to 提供单数,如下所示:

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

      完整的关联教程https://kolosek.com/rails-join-table/

      【讨论】:

        【解决方案4】:

        也发生在我身上。 通过将连接表作为 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!
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-24
          • 1970-01-01
          • 2011-11-27
          • 1970-01-01
          • 2015-11-16
          • 2014-12-16
          相关资源
          最近更新 更多