多态 has_and_belongs_to_many

2023-12-30

如何定义 has_and_belongs_to_many 多态关联?

情况:

想象一下我们有用户、曲目、列表等......并且所有这些模型都可以被标记并使用此标签进行过滤。

我想做的是:

Use has_and_belongs_to_many这使得标签可以拥有其他对象,并且其他对象也可以拥有其他标签。

因此,为了启用属于多种对象(用户或曲目或曲目列表)的标签,我们需要使用多态关系。

这是我此刻的代码:

标签迁移

class CreateTags < ActiveRecord::Migration[5.2]
  def change
    create_table :tags do |t|
      t.string :name
      t.references :taggable, polymorphic: true, index: true
      t.timestamps
    end
    create_table :assemblies_parts, id: false do |t|
      t.belongs_to :assembly, index: true
      t.belongs_to :part, index: true
    end
  end
end

标签模型

class Tag < ApplicationRecord
  has_and_belongs_to_many :taggable, polymorphic: true
end

用户模型

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  has_and_belongs_to_many :tags, as: :taggable
  has_one :top_list
  has_many :tracks, through: :top_list

end

轨道模型

class Track < ApplicationRecord
  has_and_belongs_to_many :tags, as: :taggable
  belongs_to :top_list
end

您可以使用has_many协会:

标签型号

class Tag < ApplicationRecord
  has_many :relation
end

关系模型

class Relation < ApplicationRecord
  belongs_to :tag
  belongs_to :taggable, polymorphic: true
end

用户模型

class User < ApplicationRecord
  has_many :relations, as: :taggable
  has_many :tags, through: :relations

  has_one :top_list
  has_many :tracks, through: :top_list
end

轨道模型

class Track < ApplicationRecord
  has_many :relations, as: :taggable
  has_many :tags, through: :relations

  belongs_to :top_list
end

标签 迁移

class CreateTags < ActiveRecord::Migration[5.2]
  def change
    create_table :tags do |t|
      t.string :name
      t.timestamps
    end
    create_table :relations, id: false do |t|
      t.references :tags, index: true
      t.references :taggable, polymorphic: true, index: true
    end
  end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

多态 has_and_belongs_to_many 的相关文章

随机推荐

  • 在64位Debian环境下编译32位qt源

    我想在 Debian 64 位环境上构建 32 位应用程序 因此 我正在尝试编译 Qt 源代码以获得 32 位库 我正在尝试使用以下配置命令 configure platform linux g 32 不幸的是 我收到如下错误 Basic
  • AngularJS 中的错误请求错误

    var successCallback function response if response success log log response data alert fetched courses and percentages su
  • 以编程方式清除 Chrome 浏览器历史记录

    我尝试过使用以下代码 但它不适用于 chrome 仅适用于旧浏览器 Browser clearHistory getContentResolver 这段代码对我有用 private void clearHistoryChrome Conte
  • 如何检测浏览器窗口是否滚动到底部?

    我需要检测用户是否滚动到页面底部 如果它们位于页面底部 当我向底部添加新内容时 我会自动将它们滚动到新底部 如果它们不在底部 则它们正在阅读页面上较高位置的先前内容 因此我不想自动滚动它们 因为它们想留在原处 如何检测用户是否滚动到页面底部
  • 启用 Sleuth 会减慢请求速度(很多)

    我将 Spring Cloud Feign 和 Sleuth 与 Zipkin 服务器结合使用 我的问题是 当我启用 Sleuth 时 任何简单的请求至少需要 600 毫秒 请注意 出于测试目的 我将 Sleuth 的采样器百分比设置为 1
  • http 代理背后的 PHP Composer

    我在网络上使用 Composer 其中访问互联网的唯一方法是使用 HTTP 或袜子代理 我有 http proxy 和 https proxy 环境变量 当 compose 尝试访问 HTTPS URL 时 我得到以下信息 file cou
  • Maven、Scala、Spring、AspectJ

    有谁知道是否可以在编译时使用aspectJ和spring编织scala类 我的编译时编织适用于我的所有 java 类 但我似乎无法让它适用于使用 Configurable 的 scala 类 作为背景 我已经为此工作了几天 多么痛苦啊 无论
  • shell 脚本中的“if [ -t 1 ]”有什么作用?

    我有将 zsh 设置为默认 shell 的代码 if t 1 then exec zsh fi 该命令具体执行什么操作if t 1 在这里做吗 我有将 zsh 设置为默认 shell 的代码 不 你没有 这不是你的代码所做的 尽管它产生了类
  • 通过 WebDAV 脚本为 NextCloud 文件添加标签

    我使用 NextCloud 11 来存储我的个人文件 并使用文档中的简单curl 脚本将文件上传到我的 NextCloud 驱动器 curl u user pw T test pdf http localhost nextcloud rem
  • 将 pdf 转换为 txt 文件的函数的输出重定向到 python 中的新文件夹

    我正在使用 python 3 我的代码使用 pdfminer 将 pdf 转换为文本 我想在新文件夹中获取这些文件的输出 目前它位于现有文件夹中 使用 pdfminer 从该文件夹转换为 txt 如何将输出重定向到不同的文件夹 我希望输出位
  • PHP $_Server 无法正常工作[重复]

    这个问题在这里已经有答案了 我有以下几行 div class social http SERVER HTTP HOST SERVER REQUEST URI div 一切都很棒 它显示了当前的链接 比方说 http www example
  • 重置 Android 文本视图最大行数

    我想制作一个可以通过用户触摸折叠的 TextView 当 TextView 折叠时 我设置textView setMaxLines 4 如何在扩展方法中清除此状态 我只能想到打电话setMaxLines 值很大 例如10000 有更好的方法
  • 并行运行多个 future,超时返回默认值

    我必须并行运行多个 future 并且程序不应崩溃或挂起 现在 我一一等待 future 如果出现 TimeoutException 则使用后备值 val future1 start future1 val future2 start fu
  • 如果提交是由标签引用而不是由分支引用,那么它是否会被垃圾收集?

    我有一个引用提交的带注释的标签 如果带注释有关系吗 并且没有分支引用那里 一段时间后提交会被垃圾收集吗 不 提交不会被垃圾收集 来自标签的引用足以使提交保持活动状态
  • eslint 使用配置了 jsconfig.json 的路径映射解决导入错误

    这是我的项目结构 src assets components constants helpers pages routes eslintrc json jsconfig json App js index js 我厌倦了 import So
  • BlockingCollection 与 Parallel.For 挂起?

    我正在玩BlockingCollection尝试更好地理解它们 但我很难理解为什么当我使用Parallel For 我只是添加一个数字 生产者 var blockingCollection new BlockingCollection
  • Request.IsLocal 替代方案?

    据我所理解 请求 IsLocal http msdn microsoft com en us library system web httprequest islocal aspx在两种情况下返回 true 如果请求发起者的IP地址是127
  • SwiftUI 自定义 PickerStyle

    我正在尝试写一个自定义PickerStyle看起来类似于SegmentedPickerStyle 这是我目前的状态 import SwiftUI public struct FilterPickerStyle PickerStyle pub
  • 使用 SocketChannel Android 连接到 websocket

    我编写了连接到 websocket 服务器 服务器应用程序和 android 应用程序的 android 应用程序Autobahn网络套接字库 我可以成功连接服务器并与服务器交换消息 但一段时间后 20 30 分钟后 Android 应用程
  • 多态 has_and_belongs_to_many

    如何定义 has and belongs to many 多态关联 情况 想象一下我们有用户 曲目 列表等 并且所有这些模型都可以被标记并使用此标签进行过滤 我想做的是 Use has and belongs to many这使得标签可以拥