mysql - 为什么代码库中没有模型定义,但数据库中有表?

标签 mysql sql ruby activerecord

我一直试图弄清楚为什么即使该表位于数据库中,我也无法在代码中找到模型。该表是多对多关系的交叉引用表。 Active Record 是否专门处理这个问题?该表仅包含两个外键和审计字段。我变得更加困惑的原因是因为我们还有其他交叉引用表,我可以看到模型。

最佳答案

has_and_belongs_to_many association here .

这不需要模型,但需要您应该检查的迁移中的条目。

摘自链接,示例模型:

class Assembly < ActiveRecord::Base
  has_and_belongs_to_many :parts
end

class Part < ActiveRecord::Base
  has_and_belongs_to_many :assemblies
end

示例迁移:

class CreateAssembliesAndParts < ActiveRecord::Migration
  def change
    create_table :assemblies do |t|
      t.string :name
      t.timestamps
    end

    create_table :parts do |t|
      t.string :part_number
      t.timestamps
    end

    create_table :assemblies_parts, id: false do |t|
      t.belongs_to :assembly
      t.belongs_to :part
    end
  end
end

关于mysql - 为什么代码库中没有模型定义,但数据库中有表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23070767/

相关文章:

mysql - 请求批准数据库模型

SQL Server 查询进行空控制

windows - 为什么它像没有线程一样运行?

ruby-on-rails - ruby rails : Best way to test a failed call to a third party API

php - 如何避免此 PDO 异常 : Cannot execute queries while other unbuffered queries are active

php - 如何使用PHP检查用户是否已经存在于MySQL中

sql - 需要查明 S​​QL Server 表中的所有列是否具有相同的值

ruby-on-rails - ruby rails : how to get error messages from a child resource displayed?

php - Yii CActiveDataProvider 数据未从连接表加载

用于自动递增 varchar(不是主键)的 MySQL 触发器