Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed that has_and_belongs_to_many didn't respect single table inheri…
…tance types #1081 [Florian Weber]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1641 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Jul 3, 2005
1 parent 38e0862 commit df95128
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 8 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Fixed that has_and_belongs_to_many didn't respect single table inheritance types #1081 [Florian Weber]

* Speed up ActiveRecord#method_missing for the common case (read_attribute).

* Only notify observers on after_find and after_initialize if these methods are defined on the model. [skaes@web.de]
Expand Down
Expand Up @@ -158,6 +158,15 @@ def construct_sql
"j.#{@association_class_primary_key_name} = #{@owner.quoted_id} "

@finder_sql << " AND #{interpolate_sql(@options[:conditions])}" if @options[:conditions]

unless @association_class.descends_from_active_record?
type_condition = @association_class.send(:subclasses).inject("t.#{@association_class.inheritance_column} = '#{@association_class.name.demodulize}' ") do |condition, subclass|
condition << "OR t.#{@association_class.inheritance_column} = '#{subclass.name.demodulize}' "
end

@finder_sql << " AND (#{type_condition})"
end

@finder_sql << " ORDER BY #{@order}" if @order
end
end
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/associations_test.rb
Expand Up @@ -1105,4 +1105,18 @@ def test_replace_on_new_object
new_developer.save
assert_equal 2, new_developer.projects.length
end

def test_consider_type
developer = Developer.find(:first)
special_project = SpecialProject.create("name" => "Special Project")

other_project = developer.projects.first
developer.special_projects << special_project
developer.reload

assert developer.projects.include?(special_project)
assert developer.special_projects.include?(special_project)
assert !developer.special_projects.include?(other_project)
end

end
2 changes: 1 addition & 1 deletion activerecord/test/base_test.rb
Expand Up @@ -114,7 +114,7 @@ def test_update_array_content
end

def test_attributes_hash
assert_equal @loaded_fixtures['projects']['active_record'].to_hash, Project.find(:first).attributes
assert_equal @loaded_fixtures['computers']['workstation'].to_hash, Computer.find(:first).attributes
end

def test_create
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/fixtures/db_definitions/db2.sql
Expand Up @@ -44,6 +44,7 @@ CREATE TABLE developers (
CREATE TABLE projects (
id int generated by default as identity (start with +10000),
name varchar(100) default NULL,
type varchar(255) default NULL,
PRIMARY KEY (id)
);

Expand Down
1 change: 1 addition & 0 deletions activerecord/test/fixtures/db_definitions/mysql.sql
Expand Up @@ -45,6 +45,7 @@ CREATE TABLE `developers` (
CREATE TABLE `projects` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) default NULL,
`type` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
) TYPE=InnoDB;

Expand Down
1 change: 1 addition & 0 deletions activerecord/test/fixtures/db_definitions/oci.sql
Expand Up @@ -63,6 +63,7 @@ create table developers (
create table projects (
id integer not null,
name varchar(100) default null,
type varchar(255) default null,
primary key (id)
);

Expand Down
1 change: 1 addition & 0 deletions activerecord/test/fixtures/db_definitions/postgresql.sql
Expand Up @@ -37,6 +37,7 @@ SELECT setval('developers_id_seq', 100);
CREATE TABLE projects (
id serial,
name character varying(100),
type varchar(255),
PRIMARY KEY (id)
);
SELECT setval('projects_id_seq', 100);
Expand Down
3 changes: 2 additions & 1 deletion activerecord/test/fixtures/db_definitions/sqlite.sql
Expand Up @@ -40,7 +40,8 @@ CREATE TABLE 'developers' (

CREATE TABLE 'projects' (
'id' INTEGER PRIMARY KEY NOT NULL,
'name' TEXT DEFAULT NULL
'name' TEXT DEFAULT NULL,
'type' VARCHAR(255) DEFAULT NULL
);

CREATE TABLE 'developers_projects' (
Expand Down
3 changes: 2 additions & 1 deletion activerecord/test/fixtures/db_definitions/sqlserver.sql
Expand Up @@ -39,7 +39,8 @@ CREATE TABLE developers (

CREATE TABLE projects (
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
name varchar(100) default NULL
name varchar(100) default NULL,
type varchar(255) default NULL
);

CREATE TABLE developers_projects (
Expand Down
3 changes: 2 additions & 1 deletion activerecord/test/fixtures/developer.rb
@@ -1,6 +1,7 @@
class Developer < ActiveRecord::Base
has_and_belongs_to_many :projects

has_and_belongs_to_many :special_projects, :join_table => 'developers_projects', :association_foreign_key => 'project_id'

validates_inclusion_of :salary, :in => 50000..200000
validates_length_of :name, :within => 3..20
end
Expand Down
3 changes: 3 additions & 0 deletions activerecord/test/fixtures/subscriber.rb
Expand Up @@ -3,3 +3,6 @@ def self.primary_key
"nick"
end
end

class SpecialSubscriber < Subscriber
end
8 changes: 4 additions & 4 deletions activerecord/test/inheritance_test.rb
@@ -1,9 +1,10 @@
require 'abstract_unit'
require 'fixtures/company'
require 'fixtures/project'
require 'fixtures/subscriber'

class InheritanceTest < Test::Unit::TestCase
fixtures :companies, :projects
fixtures :companies, :projects, :subscribers

def test_a_bad_type_column
#SQLServer need to turn Identity Insert On before manually inserting into the Identity column
Expand Down Expand Up @@ -125,9 +126,8 @@ def test_alt_complex_inheritance
end

def test_inheritance_without_mapping
assert_kind_of SpecialProject, SpecialProject.find(1)
assert_nothing_raised { SpecialProject.create("name" => "And breaaaaathe!") }

assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132")
assert_nothing_raised { SpecialSubscriber.create("name" => "And breaaaaathe!", "id" => "smartass") }
end

private
Expand Down

0 comments on commit df95128

Please sign in to comment.