HABTM で使う中間テーブル名と crate_join_table
Rails4.1.4 あたり
class CreateJoinTableTodoPost < ActiveRecord::Migration def change create_join_table :user_posts, user_todos do |t| end end end
をmigrateすると user_posts_user_todos というテーブルが作られる。
でもhas_and_belongs_to_many :user_todos を宣言した時に参照される中間テーブルは、user_posts_todos。
共通のプレフィックス削除は仕様のようで、
create_join_tableヘルパーが命名規則に従っていなかったっぽい。
4.2にはこの修正が入っているみたい。
https://github.com/rails/rails/commit/a013b082aa4459762b55bbc6c59a4f072eef8224
4.2以前で対応する場合は create_tableでプレフィックスを削除したテーブル名を明示しないとだめ。
class CreateJoinTableTodoPost < ActiveRecord::Migration def change create_table :user_posts_todos, id: false do |t| end end end