我一直试图弄清楚为什么即使该表位于数据库中,我也无法在代码中找到模型。该表是多对多关系的交叉引用表。 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/