0
votes

I want to create a M2M Association for two models, one is in a different namespace than the other, when I try to access the methods added by has_and_belongs_to_many from either class, rails tells me

ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column namespace_model1s_model2s.model1_id does not exist

Here're my model:

scope/model1.rb

class Namespace::Model1 < ActiveRecord::Base
    has_and_belongs_to_many :model2s
end

model2.rb

class Model2 < ActiveRecord::Base
    has_and_belongs_to_many :namespace_model1s
end

generated migration file from executing this command rails g migration CreateJoinTableNamespaceModel1sModel2s namespace_model1 model2

class CreateJoinTableNamespaceModel1sModel2s < ActiveRecord::Migration
  def change
    create_join_table :namespace_model1s, :model2s do |t|
      # t.index [:namespace_model1_id, :model2_id]
      # t.index [:model2_id, :namespace_model1_id]
    end
  end
end

And finaly the resulting schema.rb

 create_table "namespace_model1s_model2s", id: false, force: :cascade do |t|
   t.integer "namespace_model1_id", null: false
   t.integer "model2_id",            null: false
 end

So, can anybody tell me where I goofed?

1

1 Answers

0
votes

So, my mistake was that I forgot to define class_name: for every call outside the namespace.