From a2f26b971bf61263582604ad7e1af14b9566949e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 18 Jun 2005 05:28:59 +0000 Subject: Fixed that adding a record to a has_and_belongs_to collection would always save it -- now it only saves if its a new record #1203 [Alisdair McDiarmid] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1453 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ .../associations/has_and_belongs_to_many_association.rb | 4 +++- activerecord/test/associations_test.rb | 16 ++++++++++++++++ activerecord/test/fixtures/db_definitions/db2.sql | 2 ++ activerecord/test/fixtures/db_definitions/mysql.sql | 2 ++ activerecord/test/fixtures/db_definitions/oci.sql | 2 ++ activerecord/test/fixtures/db_definitions/postgresql.sql | 2 ++ activerecord/test/fixtures/db_definitions/sqlite.sql | 4 +++- activerecord/test/fixtures/db_definitions/sqlserver.sql | 4 +++- 9 files changed, 35 insertions(+), 3 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index e49e96e738..4f7f69220e 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed that adding a record to a has_and_belongs_to collection would always save it -- now it only saves if its a new record #1203 [Alisdair McDiarmid] + * Fixed saving of in-memory association structures to happen as a after_create/after_update callback instead of after_save -- that way you can add new associations in after_create/after_update callbacks without getting them saved twice * Allow any Enumerable, not just Array, to work as bind variables #1344 [Jeremy Kemper] 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 42a4f3495d..b34574161f 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 @@ -104,7 +104,9 @@ module ActiveRecord end def insert_record(record) - return false unless record.save + if record.new_record? + return false unless record.save + end if @options[:insert_sql] @owner.connection.execute(interpolate_sql(@options[:insert_sql], record)) diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 02212a862b..214b582f56 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -823,6 +823,22 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase assert_equal 2, action_controller.developers(true).size end + def test_adding_from_the_project_fixed_timestamp + jamis = Developer.find(2) + action_controller = Project.find(2) + action_controller.developers.reload + assert_equal 1, jamis.projects.size + assert_equal 1, action_controller.developers.size + updated_at = jamis.updated_at + + action_controller.developers << jamis + + assert_equal updated_at, jamis.updated_at + assert_equal 2, jamis.projects(true).size + assert_equal 2, action_controller.developers.size + assert_equal 2, action_controller.developers(true).size + end + def test_adding_multiple aredridel = Developer.new("name" => "Aredridel") aredridel.save diff --git a/activerecord/test/fixtures/db_definitions/db2.sql b/activerecord/test/fixtures/db_definitions/db2.sql index 6d13c73fbe..bd1696d546 100644 --- a/activerecord/test/fixtures/db_definitions/db2.sql +++ b/activerecord/test/fixtures/db_definitions/db2.sql @@ -36,6 +36,8 @@ CREATE TABLE developers ( id int generated by default as identity (start with +10000), name varchar(100) default NULL, salary int default 70000, + created_at timestamp default NULL, + updated_at timestamp default NULL, PRIMARY KEY (id) ); diff --git a/activerecord/test/fixtures/db_definitions/mysql.sql b/activerecord/test/fixtures/db_definitions/mysql.sql index 5a24ea3768..6fa1a38615 100755 --- a/activerecord/test/fixtures/db_definitions/mysql.sql +++ b/activerecord/test/fixtures/db_definitions/mysql.sql @@ -37,6 +37,8 @@ CREATE TABLE `developers` ( `id` int(11) NOT NULL auto_increment, `name` varchar(100) default NULL, `salary` int(11) default 70000, + `created_at` datetime default NULL, + `updated_at` datetime default NULL, PRIMARY KEY (`id`) ) TYPE=InnoDB; diff --git a/activerecord/test/fixtures/db_definitions/oci.sql b/activerecord/test/fixtures/db_definitions/oci.sql index 5650c93475..be6872a26a 100644 --- a/activerecord/test/fixtures/db_definitions/oci.sql +++ b/activerecord/test/fixtures/db_definitions/oci.sql @@ -55,6 +55,8 @@ create table developers ( id integer not null, name varchar(100) default null, salary integer default 70000, + created_at timestamp default null, + updated_at timestamp default null, primary key (id) ); diff --git a/activerecord/test/fixtures/db_definitions/postgresql.sql b/activerecord/test/fixtures/db_definitions/postgresql.sql index 134f030f13..60623e5a48 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.sql +++ b/activerecord/test/fixtures/db_definitions/postgresql.sql @@ -28,6 +28,8 @@ CREATE TABLE developers ( id serial, name character varying(100), salary integer DEFAULT 70000, + created_at timestamp, + updated_at timestamp, PRIMARY KEY (id) ); SELECT setval('developers_id_seq', 100); diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql index 07d2ea33fd..08a515e67b 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite.sql @@ -33,7 +33,9 @@ CREATE TABLE 'topics' ( CREATE TABLE 'developers' ( 'id' INTEGER PRIMARY KEY NOT NULL, 'name' TEXT DEFAULT NULL, - 'salary' INTEGER DEFAULT 70000 + 'salary' INTEGER DEFAULT 70000, + 'created_at' DATETIME DEFAULT NULL, + 'updated_at' DATETIME DEFAULT NULL ); CREATE TABLE 'projects' ( diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.sql b/activerecord/test/fixtures/db_definitions/sqlserver.sql index c96114de89..9c5658e387 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.sql +++ b/activerecord/test/fixtures/db_definitions/sqlserver.sql @@ -32,7 +32,9 @@ CREATE TABLE topics ( CREATE TABLE developers ( id int NOT NULL IDENTITY(1, 1) PRIMARY KEY, name varchar(100) default NULL, - salary int default 70000 + salary int default 70000, + created_at datetime default NULL, + updated_at datetime default NULL ); CREATE TABLE projects ( -- cgit v1.2.3