mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ensure HABTM relationships produce valid class names (Fixes #17119)
This commit is contained in:
parent
47704af54d
commit
f43f56e16e
7 changed files with 25 additions and 3 deletions
|
@ -1700,7 +1700,7 @@ module ActiveRecord
|
||||||
hm_options[:through] = middle_reflection.name
|
hm_options[:through] = middle_reflection.name
|
||||||
hm_options[:source] = join_model.right_reflection.name
|
hm_options[:source] = join_model.right_reflection.name
|
||||||
|
|
||||||
[:before_add, :after_add, :before_remove, :after_remove, :autosave, :validate, :join_table].each do |k|
|
[:before_add, :after_add, :before_remove, :after_remove, :autosave, :validate, :join_table, :class_name].each do |k|
|
||||||
hm_options[k] = options[k] if options.key? k
|
hm_options[k] = options[k] if options.key? k
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -98,7 +98,7 @@ module ActiveRecord::Associations::Builder
|
||||||
|
|
||||||
def middle_options(join_model)
|
def middle_options(join_model)
|
||||||
middle_options = {}
|
middle_options = {}
|
||||||
middle_options[:class] = join_model
|
middle_options[:class_name] = "#{lhs_model.name}::#{join_model.name}"
|
||||||
middle_options[:source] = join_model.left_reflection.name
|
middle_options[:source] = join_model.left_reflection.name
|
||||||
if options.key? :foreign_key
|
if options.key? :foreign_key
|
||||||
middle_options[:foreign_key] = options[:foreign_key]
|
middle_options[:foreign_key] = options[:foreign_key]
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
require "cases/helper"
|
require "cases/helper"
|
||||||
require 'models/developer'
|
require 'models/developer'
|
||||||
|
require 'models/computer'
|
||||||
require 'models/project'
|
require 'models/project'
|
||||||
require 'models/company'
|
require 'models/company'
|
||||||
require 'models/customer'
|
require 'models/customer'
|
||||||
|
@ -80,7 +81,7 @@ end
|
||||||
|
|
||||||
class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
||||||
fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects,
|
fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects,
|
||||||
:parrots, :pirates, :parrots_pirates, :treasures, :price_estimates, :tags, :taggings
|
:parrots, :pirates, :parrots_pirates, :treasures, :price_estimates, :tags, :taggings, :computers
|
||||||
|
|
||||||
def setup_data_for_habtm_case
|
def setup_data_for_habtm_case
|
||||||
ActiveRecord::Base.connection.execute('delete from countries_treaties')
|
ActiveRecord::Base.connection.execute('delete from countries_treaties')
|
||||||
|
@ -883,4 +884,12 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
||||||
child.special_projects << SpecialProject.new("name" => "Special Project")
|
child.special_projects << SpecialProject.new("name" => "Special Project")
|
||||||
assert child.save, 'child object should be saved'
|
assert child.save, 'child object should be saved'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_habtm_with_reflection_using_class_name_and_fixtures
|
||||||
|
assert_not_nil Developer._reflections['shared_computers']
|
||||||
|
# Checking the fixture for named association is important here, because it's the only way
|
||||||
|
# we've been able to reproduce this bug
|
||||||
|
assert_not_nil File.read(File.expand_path("../../../fixtures/developers.yml", __FILE__)).index("shared_computers")
|
||||||
|
assert_equal developers(:david).shared_computers.first, computers(:laptop)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
5
activerecord/test/fixtures/computers.yml
vendored
5
activerecord/test/fixtures/computers.yml
vendored
|
@ -3,3 +3,8 @@ workstation:
|
||||||
system: 'Linux'
|
system: 'Linux'
|
||||||
developer: 1
|
developer: 1
|
||||||
extendedWarranty: 1
|
extendedWarranty: 1
|
||||||
|
|
||||||
|
laptop:
|
||||||
|
system: 'MacOS 1'
|
||||||
|
developer: 1
|
||||||
|
extendedWarranty: 1
|
||||||
|
|
1
activerecord/test/fixtures/developers.yml
vendored
1
activerecord/test/fixtures/developers.yml
vendored
|
@ -2,6 +2,7 @@ david:
|
||||||
id: 1
|
id: 1
|
||||||
name: David
|
name: David
|
||||||
salary: 80000
|
salary: 80000
|
||||||
|
shared_computers: laptop
|
||||||
|
|
||||||
jamis:
|
jamis:
|
||||||
id: 2
|
id: 2
|
||||||
|
|
|
@ -15,6 +15,8 @@ class Developer < ActiveRecord::Base
|
||||||
|
|
||||||
accepts_nested_attributes_for :projects
|
accepts_nested_attributes_for :projects
|
||||||
|
|
||||||
|
has_and_belongs_to_many :shared_computers, class_name: "Computer"
|
||||||
|
|
||||||
has_and_belongs_to_many :projects_extended_by_name,
|
has_and_belongs_to_many :projects_extended_by_name,
|
||||||
-> { extending(DeveloperProjectsAssociationExtension) },
|
-> { extending(DeveloperProjectsAssociationExtension) },
|
||||||
:class_name => "Project",
|
:class_name => "Project",
|
||||||
|
|
|
@ -228,6 +228,11 @@ ActiveRecord::Schema.define do
|
||||||
t.integer :extendedWarranty, null: false
|
t.integer :extendedWarranty, null: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table :computers_developers, id: false, force: true do |t|
|
||||||
|
t.references :computer
|
||||||
|
t.references :developer
|
||||||
|
end
|
||||||
|
|
||||||
create_table :contracts, force: true do |t|
|
create_table :contracts, force: true do |t|
|
||||||
t.integer :developer_id
|
t.integer :developer_id
|
||||||
t.integer :company_id
|
t.integer :company_id
|
||||||
|
|
Loading…
Reference in a new issue