ruby-on-rails - Rails 正在寻找错误的连接表

标签 ruby-on-rails

我有两个以相同名称开头的模型:LawFieldLawCategory,它们之间存在 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/

相关文章:

mysql - 缺少 "C:\Program Files\MySQL\MySQL Server X.X"文件夹

java - 同时使用 java 和 Ruby on Rails...可能吗?

ruby-on-rails - 为什么这些默认参数按原样定义?

ruby-on-rails - 分割YouTube网址 rails

sql - ruby on rails update_attributes 是否可以防止 sql 注入(inject)?

javascript - 使用 Yarn on Rails 安装 Modernizr

ruby-on-rails - 使用 xpath 在 selenium 中检索文本?

javascript - 如果没有 "link_to"类助手,我如何调用 ajax?

ruby-on-rails - 在 View 中使用 fields_for 的 Rails 3 不起作用

ruby-on-rails - 为 Rails 应用程序设置 httpd.conf