Fix has_and_belongs_to_many in a namespaced model pointing to a non namespaced model

Now the following case will work fine

    class Tag < ActiveRecord::Base
    end

    class Publisher::Article < ActiveRecord::Base
      has_and_belongs_to_many :tags
    end

Fixes #15761
上级 49837346
......@@ -15,7 +15,10 @@ def join_table
end
private
def klass; @rhs_class_name.constantize; end
def klass
@lhs_class.send(:compute_type, @rhs_class_name)
end
end
def self.build(lhs_class, name, options)
......@@ -23,13 +26,7 @@ def self.build(lhs_class, name, options)
KnownTable.new options[:join_table].to_s
else
class_name = options.fetch(:class_name) {
model_name = name.to_s.camelize.singularize
if lhs_class.parent_name
model_name.prepend("#{lhs_class.parent_name}::")
end
model_name
name.to_s.camelize.singularize
}
KnownClass.new lhs_class, class_name
end
......
......@@ -869,6 +869,15 @@ def test_namespaced_habtm
assert_includes magazine.articles, article
end
def test_has_and_belongs_to_many_in_a_namespaced_model_pointing_to_a_non_namespaced_model
article = Publisher::Article.create
tag = Tag.create
article.tags << tag
article.save
assert_includes article.tags, tag
end
def test_redefine_habtm
child = SubDeveloper.new("name" => "Aredridel")
child.special_projects << SpecialProject.new("name" => "Special Project")
......
class Publisher::Article < ActiveRecord::Base
has_and_belongs_to_many :magazines
has_and_belongs_to_many :tags
end
......@@ -62,6 +62,11 @@ def except(adapter_names_to_exclude)
t.references :magazine
end
create_table :articles_tags, force: true do |t|
t.references :article
t.references :tag
end
create_table :audit_logs, force: true do |t|
t.column :message, :string, null: false
t.column :developer_id, :integer, null: false
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册