Creating associations between namespaced models has always left me confused
It’s not straightforward whether reference columns, association names, etc; need to include the namespace as a prefix or not:
> rails g model Admin::Post title content > rails db:migrate > rails g model Admin::Comment text post:belongs_to > rails db:migrate ❯ rails runner "Admin::Comment.create(text: 'hello', post: Admin::Post.create!(title: 'Hello World'))" ... 1.4.2/lib/sqlite3/database.rb:147:in `initialize': no such table: main.posts (SQLite3::SQLException) ❯ rails g model Admin::Commentt text admin_post:belongs_to > rails db:migrate ❯ rails runner "Admin::Commentt.create(text: 'hello', admin_post: Admin::Post.create!(title: 'Hello World'))" uninitialized constant Admin::Commentt::AdminPost
The last example only starts working after editing the model:
class Admin::Commentt < ApplicationRecord belongs_to :admin_post, class_name: 'Admin::Post' end
So, I don’t know if this is a ruby constant resolution problem, or a bug with Rails, but it’s very confusing! and seems Rails is trying to resolve the constant correctly but not being able to.