加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails 4找不到关联has_many,通过:关系错误

发布时间:2020-12-16 22:32:48 所属栏目:百科 来源:网络整理
导读:对.这只是拒绝工作.在这几个小时. 专辑模特儿 class Album ActiveRecord::Base has_many :features,through: :join_table1end 特征模型 class Feature ActiveRecord::Base has_many :albums,through: :join_table1end join_table1模型 class JoinTable1 Acti
对.这只是拒绝工作.在这几个小时.

专辑模特儿

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: Could not find the association :join_table1 in model Album

为什么是这样?

解决方法

您需要添加has_many:album_features到Album和Feature模型,如:通过引用关联 – 您的错误正是关于它.

或者你可以使用has_and_belongs_to_many,所以不需要定义特殊的链接模型.但是在这种情况下,您必须将表“album_features”命名.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读