From 83e2f6ae1e1d4c6863b7c0514d55d7641fbd7513 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Mon, 18 Jul 2005 11:06:41 +0000 Subject: Allow unspecified join-table columns to use to their default values when adding to a habtm collection git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1860 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../has_and_belongs_to_many_association.rb | 6 ++++-- activerecord/test/associations_test.rb | 20 ++++++++++++++++++++ activerecord/test/fixtures/db_definitions/db2.sql | 3 ++- activerecord/test/fixtures/db_definitions/mysql.sql | 3 ++- activerecord/test/fixtures/db_definitions/oci.sql | 3 ++- .../test/fixtures/db_definitions/postgresql.sql | 3 ++- activerecord/test/fixtures/db_definitions/sqlite.sql | 3 ++- .../test/fixtures/db_definitions/sqlserver.sql | 3 ++- 8 files changed, 36 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index 9183445d74..107f3ef12c 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -122,8 +122,10 @@ module ActiveRecord when @association_foreign_key attributes[column.name] = record.quoted_id else - value = @owner.send(:quote, record[column.name], column) - attributes[column.name] = value unless value.nil? + if record.attributes.has_key?(column.name) + value = @owner.send(:quote, record[column.name], column) + attributes[column.name] = value unless value.nil? + end end attributes end diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 25b5457051..f922359d0a 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -883,6 +883,26 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase assert_equal 2, aredridel.projects(true).size end + def test_adding_uses_default_values_on_join_table + ac = projects(:action_controller) + assert !developers(:jamis).projects.include?(ac) + developers(:jamis).projects << ac + + assert developers(:jamis, :reload).projects.include?(ac) + project = developers(:jamis).projects.detect { |p| p == ac } + assert_equal 1, project.access_level.to_i + end + + def test_adding_uses_explicit_values_on_join_table + ac = projects(:action_controller) + assert !developers(:jamis).projects.include?(ac) + developers(:jamis).projects.push_with_attributes(ac, :access_level => 3) + + assert developers(:jamis, :reload).projects.include?(ac) + project = developers(:jamis).projects.detect { |p| p == ac } + assert_equal 3, project.access_level.to_i + end + def test_habtm_adding_before_save no_of_devels = Developer.count no_of_projects = Project.count diff --git a/activerecord/test/fixtures/db_definitions/db2.sql b/activerecord/test/fixtures/db_definitions/db2.sql index 90f57a0c08..5977dc4bb2 100644 --- a/activerecord/test/fixtures/db_definitions/db2.sql +++ b/activerecord/test/fixtures/db_definitions/db2.sql @@ -51,7 +51,8 @@ CREATE TABLE projects ( CREATE TABLE developers_projects ( developer_id int NOT NULL, project_id int NOT NULL, - joined_on date default NULL + joined_on date default NULL, + access_level smallint default 1 ); CREATE TABLE customers ( diff --git a/activerecord/test/fixtures/db_definitions/mysql.sql b/activerecord/test/fixtures/db_definitions/mysql.sql index d1d65194cb..1400f1608b 100755 --- a/activerecord/test/fixtures/db_definitions/mysql.sql +++ b/activerecord/test/fixtures/db_definitions/mysql.sql @@ -52,7 +52,8 @@ CREATE TABLE `projects` ( CREATE TABLE `developers_projects` ( `developer_id` int(11) NOT NULL, `project_id` int(11) NOT NULL, - `joined_on` date default NULL + `joined_on` date default NULL, + `access_level` smallint default 1 ) TYPE=InnoDB; CREATE TABLE `customers` ( diff --git a/activerecord/test/fixtures/db_definitions/oci.sql b/activerecord/test/fixtures/db_definitions/oci.sql index 43a54df604..0923651dfc 100644 --- a/activerecord/test/fixtures/db_definitions/oci.sql +++ b/activerecord/test/fixtures/db_definitions/oci.sql @@ -70,7 +70,8 @@ create table projects ( create table developers_projects ( developer_id integer not null references developers initially deferred disable, project_id integer not null references projects initially deferred disable, - joined_on timestamp default null + joined_on timestamp default null, + access_level integer default 1 ); -- Try again for 8i create table developers_projects ( diff --git a/activerecord/test/fixtures/db_definitions/postgresql.sql b/activerecord/test/fixtures/db_definitions/postgresql.sql index 37f3ae5f29..9d2e50132a 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.sql +++ b/activerecord/test/fixtures/db_definitions/postgresql.sql @@ -21,7 +21,8 @@ SELECT setval('companies_id_seq', 100); CREATE TABLE developers_projects ( developer_id integer NOT NULL, project_id integer NOT NULL, - joined_on date + joined_on date, + access_level integer default 1 ); CREATE TABLE developers ( diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql index ab06d4a4d7..2ffe91324c 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite.sql @@ -47,7 +47,8 @@ CREATE TABLE 'projects' ( CREATE TABLE 'developers_projects' ( 'developer_id' INTEGER NOT NULL, 'project_id' INTEGER NOT NULL, - 'joined_on' DATE DEFAULT NULL + 'joined_on' DATE DEFAULT NULL, + 'access_level' INTEGER DEFAULT 1 ); CREATE TABLE 'customers' ( diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.sql b/activerecord/test/fixtures/db_definitions/sqlserver.sql index 6a15c0cbfd..e5123ccef0 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.sql +++ b/activerecord/test/fixtures/db_definitions/sqlserver.sql @@ -46,7 +46,8 @@ CREATE TABLE projects ( CREATE TABLE developers_projects ( developer_id int NOT NULL, project_id int NOT NULL, - joined_on datetime default NULL + joined_on datetime default NULL, + access_level int default 1 ); CREATE TABLE customers ( -- cgit v1.2.3