From 83c1934003740ed01c618a8943457a0df53e2adb Mon Sep 17 00:00:00 2001 From: mattbauer Date: Sun, 21 Jun 2009 17:35:04 +0100 Subject: Ensure hm:t#create respects source associations hash conditions [#2090 state:resolved] Signed-off-by: Pratik Naik --- .../lib/active_record/associations/through_association_scope.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/through_association_scope.rb b/activerecord/lib/active_record/associations/through_association_scope.rb index 7661c50039..8e7ce33814 100644 --- a/activerecord/lib/active_record/associations/through_association_scope.rb +++ b/activerecord/lib/active_record/associations/through_association_scope.rb @@ -94,10 +94,17 @@ module ActiveRecord def construct_join_attributes(associate) # TODO: revist this to allow it for deletion, supposing dependent option is supported raise ActiveRecord::HasManyThroughCantAssociateThroughHasManyReflection.new(@owner, @reflection) if @reflection.source_reflection.macro == :has_many + join_attributes = construct_owner_attributes(@reflection.through_reflection).merge(@reflection.source_reflection.primary_key_name => associate.id) + if @reflection.options[:source_type] join_attributes.merge!(@reflection.source_reflection.options[:foreign_type] => associate.class.base_class.name.to_s) end + + if @reflection.through_reflection.options[:conditions].is_a?(Hash) + join_attributes.merge!(@reflection.through_reflection.options[:conditions]) + end + join_attributes end -- cgit v1.2.3 From b26c2c11ab47dbd4d1e26806d1e861edc7d697e0 Mon Sep 17 00:00:00 2001 From: Brian Hogan Date: Thu, 19 Mar 2009 21:24:18 -0500 Subject: Ensure table names are quoted while renaming for sqlite3 adapter [#2272 state:resolved] Signed-off-by: Pratik Naik --- activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index c9d0c9574f..d96c6d6b51 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -245,7 +245,7 @@ module ActiveRecord end def rename_table(name, new_name) - execute "ALTER TABLE #{name} RENAME TO #{new_name}" + execute "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}" end # See: http://www.sqlite.org/lang_altertable.html -- cgit v1.2.3 From eb30e4ca40370b416566f1bbaf0204bb379883e4 Mon Sep 17 00:00:00 2001 From: Joseph Wilk Date: Thu, 12 Mar 2009 13:21:22 +0000 Subject: Fixed a bug where create_table could not be called without a block [#2221 state:resolved] Signed-off-by: Pratik Naik --- .../lib/active_record/connection_adapters/abstract/schema_statements.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index ff63ea3a2e..f44cd0bd5a 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -101,7 +101,7 @@ module ActiveRecord table_definition = TableDefinition.new(self) table_definition.primary_key(options[:primary_key] || Base.get_primary_key(table_name)) unless options[:id] == false - yield table_definition + yield table_definition if block_given? if options[:force] && table_exists?(table_name) drop_table(table_name, options) -- cgit v1.2.3 From 4851ca9e13a4317342df02ae25b1929340523f7a Mon Sep 17 00:00:00 2001 From: Patrick Joyce Date: Sun, 21 Jun 2009 22:07:23 +0100 Subject: Generate proper :counter_sql from :finder_sql when there is a newline character immediately following 'SELECT' [#2118 state:resolved] Signed-off-by: Pratik Naik --- .../active_record/associations/association_collection.rb | 14 +++++++++++++- .../associations/has_and_belongs_to_many_association.rb | 10 +--------- .../lib/active_record/associations/has_many_association.rb | 10 +--------- .../associations/has_many_through_association.rb | 10 +--------- 4 files changed, 16 insertions(+), 28 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index e12f6be35d..84edaec15e 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -351,7 +351,19 @@ module ActiveRecord protected def construct_find_options!(options) end - + + def construct_counter_sql + if @reflection.options[:counter_sql] + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + elsif @reflection.options[:finder_sql] + # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ + @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT\b(\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + else + @counter_sql = @finder_sql + end + end + def load_target if !@owner.new_record? || foreign_key_present begin 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 af9ce3dfb2..fd23e59e82 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 @@ -85,15 +85,7 @@ module ActiveRecord @join_sql = "INNER JOIN #{@owner.connection.quote_table_name @reflection.options[:join_table]} ON #{@reflection.quoted_table_name}.#{@reflection.klass.primary_key} = #{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.association_foreign_key}" - if @reflection.options[:counter_sql] - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - elsif @reflection.options[:finder_sql] - # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ - @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - else - @counter_sql = @finder_sql - end + construct_counter_sql end def construct_scope diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 73dd50dd07..e4b631bc54 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -97,15 +97,7 @@ module ActiveRecord @finder_sql << " AND (#{conditions})" if conditions end - if @reflection.options[:counter_sql] - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - elsif @reflection.options[:finder_sql] - # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ - @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - else - @counter_sql = @finder_sql - end + construct_counter_sql end def construct_scope diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 67631f0120..e21ef90391 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -90,15 +90,7 @@ module ActiveRecord @finder_sql = construct_conditions end - if @reflection.options[:counter_sql] - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - elsif @reflection.options[:finder_sql] - # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ - @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - else - @counter_sql = @finder_sql - end + construct_counter_sql end def has_cached_counter? -- cgit v1.2.3 From 80f1f863cd0f9cba89079511282de5710a2e1832 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Mon, 22 Jun 2009 12:04:02 -0700 Subject: Revert "Generate proper :counter_sql from :finder_sql when there is a newline character immediately following 'SELECT' [#2118 state:resolved]" This reverts commit 4851ca9e13a4317342df02ae25b1929340523f7a. The tests do not pass for postgresql. --- .../active_record/associations/association_collection.rb | 14 +------------- .../associations/has_and_belongs_to_many_association.rb | 10 +++++++++- .../lib/active_record/associations/has_many_association.rb | 10 +++++++++- .../associations/has_many_through_association.rb | 10 +++++++++- 4 files changed, 28 insertions(+), 16 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 84edaec15e..e12f6be35d 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -351,19 +351,7 @@ module ActiveRecord protected def construct_find_options!(options) end - - def construct_counter_sql - if @reflection.options[:counter_sql] - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - elsif @reflection.options[:finder_sql] - # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ - @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT\b(\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - else - @counter_sql = @finder_sql - end - end - + def load_target if !@owner.new_record? || foreign_key_present begin 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 fd23e59e82..af9ce3dfb2 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 @@ -85,7 +85,15 @@ module ActiveRecord @join_sql = "INNER JOIN #{@owner.connection.quote_table_name @reflection.options[:join_table]} ON #{@reflection.quoted_table_name}.#{@reflection.klass.primary_key} = #{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.association_foreign_key}" - construct_counter_sql + if @reflection.options[:counter_sql] + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + elsif @reflection.options[:finder_sql] + # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ + @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + else + @counter_sql = @finder_sql + end end def construct_scope diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index e4b631bc54..73dd50dd07 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -97,7 +97,15 @@ module ActiveRecord @finder_sql << " AND (#{conditions})" if conditions end - construct_counter_sql + if @reflection.options[:counter_sql] + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + elsif @reflection.options[:finder_sql] + # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ + @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + else + @counter_sql = @finder_sql + end end def construct_scope diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index e21ef90391..67631f0120 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -90,7 +90,15 @@ module ActiveRecord @finder_sql = construct_conditions end - construct_counter_sql + if @reflection.options[:counter_sql] + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + elsif @reflection.options[:finder_sql] + # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ + @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + else + @counter_sql = @finder_sql + end end def has_cached_counter? -- cgit v1.2.3 From 53a3eaa8603cf6e7a3c007f327fe5d3bb68de1ee Mon Sep 17 00:00:00 2001 From: Michael Schuerig Date: Sun, 5 Apr 2009 01:19:29 +0200 Subject: Translate adapter errors that indicate a violated uniqueness constraint to ActiveRecord::RecordNotUnique exception derived from ActiveReecord::StatementInvalid. Signed-off-by: Michael Koziarski --- activerecord/lib/active_record/base.rb | 4 ++++ .../lib/active_record/connection_adapters/abstract_adapter.rb | 7 ++++++- .../lib/active_record/connection_adapters/mysql_adapter.rb | 11 +++++++++++ .../active_record/connection_adapters/postgresql_adapter.rb | 9 +++++++++ .../lib/active_record/connection_adapters/sqlite_adapter.rb | 10 ++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 39fac0b7e7..defe1b56b6 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -67,6 +67,10 @@ module ActiveRecord #:nodoc: class StatementInvalid < ActiveRecordError end + # Raised when a record cannot be inserted because it would violate a uniqueness constraint. + class RecordNotUnique < StatementInvalid + end + # Raised when number of bind variables in statement given to :condition key (for example, when using +find+ method) # does not match number of expected variables. # diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 91b111ab55..c533d4cdb6 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -211,9 +211,14 @@ module ActiveRecord @last_verification = 0 message = "#{e.class.name}: #{e.message}: #{sql}" log_info(message, name, 0) - raise ActiveRecord::StatementInvalid, message + raise translate_exception(e, message) end + def translate_exception(e, message) + # override in derived class + ActiveRecord::StatementInvalid.new(message) + end + def format_log_entry(message, dump = nil) if ActiveRecord::Base.colorize_logging if @@row_even diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index d5536e4d67..59bf01e774 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -563,6 +563,17 @@ module ActiveRecord where_sql end + protected + + def translate_exception(exception, message) + case exception.errno + when 1062 + RecordNotUnique.new(message) + else + super + end + end + private def connect encoding = @config[:encoding] diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 18b499904e..e990052e09 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -941,6 +941,15 @@ module ActiveRecord end end + def translate_exception(exception, message) + case exception.message + when /duplicate key value violates unique constraint/ + RecordNotUnique.new(message) + else + super + end + end + private # The internal PostgreSQL identifier of the money data type. MONEY_COLUMN_TYPE_OID = 790 #:nodoc: diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index d96c6d6b51..1c2688ba9d 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -431,6 +431,16 @@ module ActiveRecord 'INTEGER PRIMARY KEY NOT NULL'.freeze end end + + def translate_exception(exception, message) + case exception.message + when /column(s)? .* (is|are) not unique/ + RecordNotUnique.new(message) + else + super + end + end + end class SQLite2Adapter < SQLiteAdapter # :nodoc: -- cgit v1.2.3 From 00a5fd3d18ac908af688c5944922cf69c56e850b Mon Sep 17 00:00:00 2001 From: Michael Schuerig Date: Sun, 5 Apr 2009 01:42:21 +0200 Subject: Translate foreign key violations to ActiveRecord::InvalidForeignKey exceptions. Signed-off-by: Michael Koziarski --- activerecord/lib/active_record/base.rb | 4 ++++ activerecord/lib/active_record/connection_adapters/mysql_adapter.rb | 2 ++ .../lib/active_record/connection_adapters/postgresql_adapter.rb | 2 ++ 3 files changed, 8 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index defe1b56b6..027ab8af9e 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -71,6 +71,10 @@ module ActiveRecord #:nodoc: class RecordNotUnique < StatementInvalid end + # Raised when a record cannot be inserted or updated because it references a non-existent record. + class InvalidForeignKey < StatementInvalid + end + # Raised when number of bind variables in statement given to :condition key (for example, when using +find+ method) # does not match number of expected variables. # diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 59bf01e774..ae065e6bc1 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -569,6 +569,8 @@ module ActiveRecord case exception.errno when 1062 RecordNotUnique.new(message) + when 1452 + InvalidForeignKey.new(message) else super end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index e990052e09..24482aa19a 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -945,6 +945,8 @@ module ActiveRecord case exception.message when /duplicate key value violates unique constraint/ RecordNotUnique.new(message) + when /violates foreign key constraint/ + InvalidForeignKey.new(message) else super end -- cgit v1.2.3 From b5dfdc714fab7d2836e0a979ca88b4a17db9ec06 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Fri, 26 Jun 2009 16:59:27 +1200 Subject: Make sure the wrapped exceptions also have the original exception available. [#2419 state:committed] --- activerecord/lib/active_record/base.rb | 15 +++++++++++++-- .../active_record/connection_adapters/mysql_adapter.rb | 4 ++-- .../connection_adapters/postgresql_adapter.rb | 4 ++-- .../active_record/connection_adapters/sqlite_adapter.rb | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 027ab8af9e..f1b7c323dc 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -67,12 +67,23 @@ module ActiveRecord #:nodoc: class StatementInvalid < ActiveRecordError end + # Parent class for all specific exceptions which wrap database driver exceptions + # provides access to the original exception also. + class WrappedDatabaseException < StatementInvalid + attr_reader :original_exception + + def initialize(message, original_exception) + super(message) + @original_exception, = original_exception + end + end + # Raised when a record cannot be inserted because it would violate a uniqueness constraint. - class RecordNotUnique < StatementInvalid + class RecordNotUnique < WrappedDatabaseException end # Raised when a record cannot be inserted or updated because it references a non-existent record. - class InvalidForeignKey < StatementInvalid + class InvalidForeignKey < WrappedDatabaseException end # Raised when number of bind variables in statement given to :condition key (for example, when using +find+ method) diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index ae065e6bc1..83cb9cff15 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -568,9 +568,9 @@ module ActiveRecord def translate_exception(exception, message) case exception.errno when 1062 - RecordNotUnique.new(message) + RecordNotUnique.new(message, exception) when 1452 - InvalidForeignKey.new(message) + InvalidForeignKey.new(message, exception) else super end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 24482aa19a..e77ae93c21 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -944,9 +944,9 @@ module ActiveRecord def translate_exception(exception, message) case exception.message when /duplicate key value violates unique constraint/ - RecordNotUnique.new(message) + RecordNotUnique.new(message, exception) when /violates foreign key constraint/ - InvalidForeignKey.new(message) + InvalidForeignKey.new(message, exception) else super end diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index 1c2688ba9d..5e5e30776a 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -435,7 +435,7 @@ module ActiveRecord def translate_exception(exception, message) case exception.message when /column(s)? .* (is|are) not unique/ - RecordNotUnique.new(message) + RecordNotUnique.new(message, exception) else super end -- cgit v1.2.3 From 7d548f795d226f57278f8bdd36298c9552421053 Mon Sep 17 00:00:00 2001 From: Justin French Date: Mon, 29 Jun 2009 15:54:09 +0200 Subject: Changed ActiveRecord::Base.human_name to underscore the class name before it humanizes it This gives you 'Post comment' rather than 'Postcomment' by default. Signed-off-by: Michael Koziarski [#2120 state:committed] --- activerecord/lib/active_record/base.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index f1b7c323dc..ff5a836b52 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1404,14 +1404,14 @@ module ActiveRecord #:nodoc: end # Transform the modelname into a more humane format, using I18n. - # Defaults to the basic humanize method. + # By default, it will underscore then humanize the class name (BlogPost.human_name #=> "Blog post"). # Default scope of the translation is activerecord.models # Specify +options+ with additional translating options. def human_name(options = {}) defaults = self_and_descendants_from_active_record.map do |klass| :"#{klass.name.underscore}" - end - defaults << self.name.humanize + end + defaults << self.name.underscore.humanize I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options)) end -- cgit v1.2.3 From db3de78a83379ab2a58e0d29fb10622b813a4d44 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Tue, 30 Jun 2009 14:37:12 -0700 Subject: Bump up the version to 3.0.pre --- activerecord/lib/active_record/version.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/version.rb b/activerecord/lib/active_record/version.rb index 852807b4c5..10ef9d7594 100644 --- a/activerecord/lib/active_record/version.rb +++ b/activerecord/lib/active_record/version.rb @@ -1,8 +1,8 @@ module ActiveRecord module VERSION #:nodoc: - MAJOR = 2 - MINOR = 3 - TINY = 2 + MAJOR = 3 + MINOR = 0 + TINY = "pre" STRING = [MAJOR, MINOR, TINY].join('.') end -- cgit v1.2.3 From 45e6f19925f23c3db257c15371d8f512cca843cd Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 30 Jun 2009 23:55:09 +0100 Subject: Revert "Revert "Generate proper :counter_sql from :finder_sql when there is a newline character immediately following 'SELECT' [#2118 state:resolved]"" This reverts commit 80f1f863cd0f9cba89079511282de5710a2e1832. The feature doesn't work on Postgres, so don't test it on Postgres. Also, Postgres compatibility is irrelevant to the ticket/patch in question. --- .../active_record/associations/association_collection.rb | 14 +++++++++++++- .../associations/has_and_belongs_to_many_association.rb | 10 +--------- .../lib/active_record/associations/has_many_association.rb | 10 +--------- .../associations/has_many_through_association.rb | 10 +--------- 4 files changed, 16 insertions(+), 28 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index e12f6be35d..84edaec15e 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -351,7 +351,19 @@ module ActiveRecord protected def construct_find_options!(options) end - + + def construct_counter_sql + if @reflection.options[:counter_sql] + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + elsif @reflection.options[:finder_sql] + # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ + @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT\b(\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + else + @counter_sql = @finder_sql + end + end + def load_target if !@owner.new_record? || foreign_key_present begin 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 af9ce3dfb2..fd23e59e82 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 @@ -85,15 +85,7 @@ module ActiveRecord @join_sql = "INNER JOIN #{@owner.connection.quote_table_name @reflection.options[:join_table]} ON #{@reflection.quoted_table_name}.#{@reflection.klass.primary_key} = #{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.association_foreign_key}" - if @reflection.options[:counter_sql] - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - elsif @reflection.options[:finder_sql] - # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ - @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - else - @counter_sql = @finder_sql - end + construct_counter_sql end def construct_scope diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 73dd50dd07..e4b631bc54 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -97,15 +97,7 @@ module ActiveRecord @finder_sql << " AND (#{conditions})" if conditions end - if @reflection.options[:counter_sql] - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - elsif @reflection.options[:finder_sql] - # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ - @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - else - @counter_sql = @finder_sql - end + construct_counter_sql end def construct_scope diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 67631f0120..e21ef90391 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -90,15 +90,7 @@ module ActiveRecord @finder_sql = construct_conditions end - if @reflection.options[:counter_sql] - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - elsif @reflection.options[:finder_sql] - # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ - @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - else - @counter_sql = @finder_sql - end + construct_counter_sql end def has_cached_counter? -- cgit v1.2.3 From 16dc139caa9286638785469304a69ab77e4fe9b5 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Wed, 1 Jul 2009 12:16:35 -0700 Subject: Added AR:B#dup method for duplicationg object without frozen attributes [#2859 state:resolved] Signed-off-by: Yehuda Katz + Carl Lerche --- activerecord/lib/active_record/base.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index ff5a836b52..deab56e219 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2870,6 +2870,13 @@ module ActiveRecord #:nodoc: @attributes.frozen? end + # Returns duplicated record with unfreezed attributes. + def dup + obj = super + obj.instance_variable_set('@attributes', instance_variable_get('@attributes').dup) + obj + end + # Returns +true+ if the record is read only. Records loaded through joins with piggy-back # attributes will be marked as read only since they cannot be saved. def readonly? -- cgit v1.2.3