Ruby on rails Rails 4找不到关联,通过:relationship error
对。这根本不起作用。我已经做了好几个小时了 专辑型号Ruby on rails Rails 4找不到关联,通过:relationship error,ruby-on-rails,ruby-on-rails-4,has-many-through,Ruby On Rails,Ruby On Rails 4,Has Many Through,对。这根本不起作用。我已经做了好几个小时了 专辑型号 class Album < ActiveRecord::Base has_many :features, through: :join_table1 end class Feature < ActiveRecord::Base has_many :albums, through: :join_table1 end class JoinTable1 < ActiveRecord::Base belongs_to
class Album < ActiveRecord::Base
has_many :features, through: :join_table1
end
class Feature < ActiveRecord::Base
has_many :albums, through: :join_table1
end
class JoinTable1 < ActiveRecord::Base
belongs_to :features
belongs_to :albums
end
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
class JoinTable1 < ActiveRecord::Base
belongs_to :features
belongs_to :albums
end
相册模式
album_id | feature_id
id | title | release_date | genre | artist_id | created_at | updated_at | price | image_path
id | feature | created_at | updated_at
功能模式
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
需要“测试助手”
类DataFlowTest
我明白了
ActiveRecord::HasManyThroughAssociationNotFoundError:在模型相册中找不到关联:join_table1
这是为什么?您需要在相册和功能模型中添加
有很多:相册功能
(假设您将JoinTable1模型重命名为更有意义的AlbumFeature,相应的表将是相册功能
),因为:通过
引用关联-您的错误就在于此
或者,您可以使用
has\u和\u belies\u to \u many
,这样就不需要定义特殊的链接模型。但是在这种情况下,您必须命名您的表albums\u features
,只需定义如下模型
专辑型号
class Album < ActiveRecord::Base
has_many :features, through: :join_table1
end
class Feature < ActiveRecord::Base
has_many :albums, through: :join_table1
end
class JoinTable1 < ActiveRecord::Base
belongs_to :features
belongs_to :albums
end
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
class JoinTable1 < ActiveRecord::Base
belongs_to :features
belongs_to :albums
end
classalbum
功能模型
class Album < ActiveRecord::Base
has_many :features, through: :join_table1
end
class Feature < ActiveRecord::Base
has_many :albums, through: :join_table1
end
class JoinTable1 < ActiveRecord::Base
belongs_to :features
belongs_to :albums
end
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
class JoinTable1 < ActiveRecord::Base
belongs_to :features
belongs_to :albums
end
class功能
加入表1模型
class Album < ActiveRecord::Base
has_many :features, through: :join_table1
end
class Feature < ActiveRecord::Base
has_many :albums, through: :join_table1
end
class JoinTable1 < ActiveRecord::Base
belongs_to :features
belongs_to :albums
end
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
class JoinTable1 < ActiveRecord::Base
belongs_to :features
belongs_to :albums
end
class JointTable1
类似于@mad_raz的回答,但联接表需要为所属对象设置奇点,如下所示:
class JoinTable1 < ActiveRecord::Base
belongs_to :feature
belongs_to :album
end
class JointTable1
完整的关联教程也发生在我身上。
通过向两个模型中添加join表使其工作。
这样地:
连接模型:
module Alerts
class AlertIncidentConnection < ActiveRecord::Base
belongs_to :incident
belongs_to :alert
end
end
模块警报
类AlertIncidentConnection
警报模型:
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
class CreateTableAlertIncidentConnections
用法:
alert.incidents << incident
alert.save!
alert.incents我认为联接表的命名约定与has\u many,through关系无关。重要的是,不能有两个具有名称功能的模型。另外,activerecord模型的名称应与其表名相对应。如果命名约定重要,您会将其命名为album\u features还是features\u album?正如@starkers所提到的,事实并非如此。我也有同样的问题,除了通过外,在相册和功能中添加has_many是答案中唯一相关的部分。@msanjay是的,你是对的,命名与has_many:through无关(虽然它与has_和_属于_many有关,但表名中的首字母顺序是用AIK字母顺序决定的),这是针对问题的第一个版本,其中有两个错误。问题已经更新,所以我也会更新答案,谢谢。