Rails has_many :通过多态关系不起作用
Rails has_many :through polymorphic relationship not working
我有以下三个模型:
事件:
class Event < ApplicationRecord
has_many :taggings, as: :taggable, dependent: :destroy
has_many :tags, through: :taggings
end
events table:
Column | Type | Modifiers
---------+----------+----------------------------------------------------
id | bigint | not null
title | string | not null
content | text | not null
标签:
class Tag < ApplicationRecord
has_many :taggings, dependent: :destory
has_many :events, through: :taggings, source: :taggable, source_type: 'Event'
end
tags table:
Column | Type | Modifiers
---------+----------+----------------------------------------------------
id | bigint | not null
name | string | not null
标记:
class Tagging < ApplicationRecord
belongs_to :taggable, polymorphic: true
belongs_to :tags
end
taggings table:
Column | Type | Modifiers
---------+----------+----------------------------------------------------
id | bigint | not null
taggable_id | integer | not null
taggable_type | string | not null
tag_id | integer | not null | FK_INDEX
现在的问题是我似乎无法使用这些关系中的任何一个,并收到指示不存在任何关联的错误。
event = Event.find(x)
event.tags
Error:
NameError: uninitialized constant Event::Tags
event.taggable
Error:
NoMethodError: undefined method `taggable' for #<Event>
我不知道是我的关联设置正确还是我的用例有误?
如何通过模式添加关联?
如何通过事件模型查询标签?
这是一个简单的复数错误:
class Tagging < ApplicationRecord
belongs_to :taggable, polymorphic: true
belongs_to :tag
end
belongs_to
和 has_one
协会的名称应始终 为单数。
如果您查看错误消息 NameError: uninitialized constant Event::Tags
,您会发现它正在寻找 Tags
而不是 Tag
。
我有以下三个模型:
事件:
class Event < ApplicationRecord
has_many :taggings, as: :taggable, dependent: :destroy
has_many :tags, through: :taggings
end
events table:
Column | Type | Modifiers
---------+----------+----------------------------------------------------
id | bigint | not null
title | string | not null
content | text | not null
标签:
class Tag < ApplicationRecord
has_many :taggings, dependent: :destory
has_many :events, through: :taggings, source: :taggable, source_type: 'Event'
end
tags table:
Column | Type | Modifiers
---------+----------+----------------------------------------------------
id | bigint | not null
name | string | not null
标记:
class Tagging < ApplicationRecord
belongs_to :taggable, polymorphic: true
belongs_to :tags
end
taggings table:
Column | Type | Modifiers
---------+----------+----------------------------------------------------
id | bigint | not null
taggable_id | integer | not null
taggable_type | string | not null
tag_id | integer | not null | FK_INDEX
现在的问题是我似乎无法使用这些关系中的任何一个,并收到指示不存在任何关联的错误。
event = Event.find(x)
event.tags
Error:
NameError: uninitialized constant Event::Tags
event.taggable
Error:
NoMethodError: undefined method `taggable' for #<Event>
我不知道是我的关联设置正确还是我的用例有误?
如何通过模式添加关联? 如何通过事件模型查询标签?
这是一个简单的复数错误:
class Tagging < ApplicationRecord
belongs_to :taggable, polymorphic: true
belongs_to :tag
end
belongs_to
和 has_one
协会的名称应始终 为单数。
如果您查看错误消息 NameError: uninitialized constant Event::Tags
,您会发现它正在寻找 Tags
而不是 Tag
。