我有两个以相同名称开头的模型:LawField
和 LawCategory
,它们之间存在 habtm
关联。我通过rails g migration CreateLawCategoriesLawFieldsJoinTable law_categories law_fields 创建了连接表。迁移看起来像这样:
class CreateLawCategoriesLawFieldsJoinTable < ActiveRecord::Migration
def change
create_join_table :law_categories, :law_fields do |t|
# t.index [:law_category_id, :law_field_id]
# t.index [:law_field_id, :law_category_id]
end
end
end
当我在 LawCategory
对象上调用 law_fields
方法时(反之亦然),我收到错误消息:
Could not find table 'law_categories_fields'
为什么 Rails 需要这个名字? (请注意单词 fields
之前缺少的 law_
)我应该这样命名表吗?为什么?我认为这是因为模型名称都以 law
开头。但我认为这没有理由会成为一个问题。
最佳答案
Am I supposed to name the table like that?
是的,是documented :
If your tables share a common prefix, it will only appear once at the beginning. For example, the tables “catalog_categories” and “catalog_products” generate a join table name of “catalog_categories_products”.
Why?
我不知道。因为比较短?因为听起来更自然?
关于ruby-on-rails - Rails 正在寻找错误的连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23106937/