From ee48cf69eeb04a21941922c7f840babe3b619c6e Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 17 Apr 2013 05:06:55 -0400 Subject: without autosave option updated records not save Emphasizing that without autosave option only new records are saved and updated records are not saved --- activerecord/lib/active_record/autosave_association.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 44323ce9db..b0bd78ad46 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -17,7 +17,8 @@ module ActiveRecord # be destroyed directly. They will however still be marked for destruction. # # Note that autosave: false is not same as not declaring :autosave. - # When the :autosave option is not present new associations are saved. + # When the :autosave option is not present then new association records are + # saved but the updated association records are not saved. # # == Validation # -- cgit v1.2.3 From 5ea8ff095133eb8a9d8fe90f26f3d14b76b3ecdf Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 17 Apr 2013 05:54:07 -0400 Subject: updated rdoc to reflect info about readonly attribute --- activerecord/lib/active_record/persistence.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 42cece3ad0..cffe7e2050 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -204,6 +204,8 @@ module ActiveRecord # * updated_at/updated_on column is updated if that column is available. # * Updates all the attributes that are dirty in this object. # + # This method raises an +ActiveRecord::ActiveRecordError+ if the + # attribute is marked as readonly. def update_attribute(name, value) name = name.to_s verify_readonly_attribute(name) -- cgit v1.2.3 From 14254d82a90b8aa4bd81f7eeebe33885bf83c378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hrvoje=20=C5=A0imi=C4=87?= Date: Wed, 17 Apr 2013 16:02:13 +0200 Subject: documentation fixes for Array.wrap and AR::Validations::AssociatedValidator --- activerecord/lib/active_record/validations/associated.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/validations/associated.rb b/activerecord/lib/active_record/validations/associated.rb index 7f1972ccf9..744780d069 100644 --- a/activerecord/lib/active_record/validations/associated.rb +++ b/activerecord/lib/active_record/validations/associated.rb @@ -9,8 +9,8 @@ module ActiveRecord end module ClassMethods - # Validates whether the associated object or objects are all valid - # themselves. Works with any kind of association. + # Validates whether the associated object or objects are all valid. + # Works with any kind of association. # # class Book < ActiveRecord::Base # has_many :pages -- cgit v1.2.3 From 5e71e92235b8a02b8594927e6ce3a3d50a28afa5 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 17 Apr 2013 06:33:33 -0400 Subject: readonly info for save and save! --- activerecord/lib/active_record/persistence.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index cffe7e2050..fbb64ad68d 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -99,6 +99,9 @@ module ActiveRecord # before_* callbacks return +false+ the action is cancelled and # +save+ returns +false+. See ActiveRecord::Callbacks for further # details. + # + # Attributes marked as readonly are silently ignored if the record is + # being updated. def save(*) create_or_update rescue ActiveRecord::RecordInvalid @@ -118,6 +121,9 @@ module ActiveRecord # the before_* callbacks return +false+ the action is cancelled # and save! raises ActiveRecord::RecordNotSaved. See # ActiveRecord::Callbacks for further details. + # + # Attributes marked as readonly are silently ignored if the record is + # being updated. def save!(*) create_or_update || raise(RecordNotSaved) end -- cgit v1.2.3 From 9693df61f0143218da6f902021734441312f9d12 Mon Sep 17 00:00:00 2001 From: Noemj Date: Tue, 30 Apr 2013 14:27:18 +0300 Subject: Moved update_record logic to relation.rb --- activerecord/lib/active_record/persistence.rb | 18 +++----------- activerecord/lib/active_record/relation.rb | 36 +++++++++++++++++++-------- 2 files changed, 28 insertions(+), 26 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 59f8e90e7a..178db07ca1 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -428,23 +428,11 @@ module ActiveRecord # Updates the associated record with values matching those of the instance attributes. # Returns the number of affected rows. def update_record(attribute_names = @attributes.keys) - attributes_with_values = arel_attributes_with_values_for_update(attribute_names) - if attributes_with_values.empty? + attributes_values = arel_attributes_with_values_for_update(attribute_names) + if attributes_values.empty? 0 else - klass = self.class - column_hash = klass.connection.schema_cache.columns_hash klass.table_name - db_columns_with_values = attributes_with_values.map { |attr,value| - real_column = column_hash[attr.name] - [real_column, value] - } - bind_attrs = attributes_with_values.dup - bind_attrs.keys.each_with_index do |column, i| - real_column = db_columns_with_values[i].first - bind_attrs[column] = klass.connection.substitute_at(real_column, i) - end - stmt = klass.unscoped.where(klass.arel_table[klass.primary_key].eq(id_was || id)).arel.compile_update(bind_attrs) - klass.connection.update stmt, 'SQL', db_columns_with_values + self.class.unscoped.update_record attributes_values, id, id_was end end diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 56462d355b..ad33e398be 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -56,16 +56,7 @@ module ActiveRecord im = arel.create_insert im.into @table - conn = @klass.connection - - substitutes = values.sort_by { |arel_attr,_| arel_attr.name } - binds = substitutes.map do |arel_attr, value| - [@klass.columns_hash[arel_attr.name], value] - end - - substitutes.each_with_index do |tuple, i| - tuple[1] = conn.substitute_at(binds[i][0], i) - end + substitutes, binds = substitute_values values if values.empty? # empty insert im.values = Arel.sql(connection.empty_insert_statement_value) @@ -73,7 +64,7 @@ module ActiveRecord im.insert substitutes end - conn.insert( + @klass.connection.insert( im, 'SQL', primary_key, @@ -82,6 +73,29 @@ module ActiveRecord binds) end + def update_record(values, id, id_was) + substitutes, binds = substitute_values values + um = @klass.unscoped.where(@klass.arel_table[@klass.primary_key].eq(id_was || id)).arel.compile_update(substitutes) + + @klass.connection.update( + um, + 'SQL', + binds) + end + + def substitute_values(values) + substitutes = values.sort_by { |arel_attr,_| arel_attr.name } + binds = substitutes.map do |arel_attr, value| + [@klass.columns_hash[arel_attr.name], value] + end + + substitutes.each_with_index do |tuple, i| + tuple[1] = @klass.connection.substitute_at(binds[i][0], i) + end + + [substitutes, binds] + end + # Initializes new record from relation while maintaining the current # scope. # -- cgit v1.2.3 From 253ccbc0acd500c25be58d0861d75d381505eb9d Mon Sep 17 00:00:00 2001 From: kennyj Date: Wed, 1 May 2013 02:21:23 +0900 Subject: Abort a rake task when missing db/structure.sql like `db:schema:load` task. --- activerecord/lib/active_record/railties/databases.rake | 8 +++----- activerecord/lib/active_record/tasks/database_tasks.rb | 8 ++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index bb9e390c8f..92bef09ff5 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -249,11 +249,8 @@ db_namespace = namespace :db do desc 'Load a schema.rb file into the database' task :load => [:environment, :load_config] do file = ENV['SCHEMA'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'schema.rb') - if File.exists?(file) - load(file) - else - abort %{#{file} doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter #{Rails.root}/config/application.rb to limit the frameworks that will be loaded.} - end + ActiveRecord::Tasks::DatabaseTasks.check_schema_file(file) + load(file) end task :load_if_ruby => ['db:create', :environment] do @@ -298,6 +295,7 @@ db_namespace = namespace :db do # desc "Recreate the databases from the structure.sql file" task :load => [:environment, :load_config] do filename = ENV['DB_STRUCTURE'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "structure.sql") + ActiveRecord::Tasks::DatabaseTasks.check_schema_file(filename) current_config = ActiveRecord::Tasks::DatabaseTasks.current_config ActiveRecord::Tasks::DatabaseTasks.structure_load(current_config, filename) end diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index 8d0792f750..3e8b79c7a0 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -148,6 +148,14 @@ module ActiveRecord class_for_adapter(configuration['adapter']).new(*arguments).structure_load(filename) end + def check_schema_file(filename) + unless File.exists?(filename) + message = %{#{filename} doesn't exist yet. Run `rake db:migrate` to create it, then try again.} + message << %{ If you do not intend to use a database, you should instead alter #{Rails.root}/config/application.rb to limit the frameworks that will be loaded.} if defined?(::Rails) + Kernel.abort message + end + end + def load_seed if seed_loader seed_loader.load_seed -- cgit v1.2.3 From 2088bf27981137a2c6c8b2f718f33b417b4045af Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 30 Apr 2013 11:20:32 -0700 Subject: mysql needs to reconnect after recreate. Thanks @mperham --- .../lib/active_record/connection_adapters/abstract_mysql_adapter.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 76c501dec5..2a7b855f95 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -365,6 +365,7 @@ module ActiveRecord def recreate_database(name, options = {}) drop_database(name) create_database(name, options) + reconnect! end # Create a new MySQL database with optional :charset and :collation. -- cgit v1.2.3 From e2a58b2921dc423c716e2a22cde4504a092c4261 Mon Sep 17 00:00:00 2001 From: Noemj Date: Tue, 30 Apr 2013 23:36:46 +0300 Subject: Added :nodoc: for private methods --- activerecord/lib/active_record/relation.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index ad33e398be..913f6f88f2 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -39,7 +39,7 @@ module ActiveRecord reset end - def insert(values) + def insert(values) # :nodoc: primary_key_value = nil if primary_key && Hash === values @@ -73,7 +73,7 @@ module ActiveRecord binds) end - def update_record(values, id, id_was) + def update_record(values, id, id_was) # :nodoc: substitutes, binds = substitute_values values um = @klass.unscoped.where(@klass.arel_table[@klass.primary_key].eq(id_was || id)).arel.compile_update(substitutes) @@ -83,7 +83,7 @@ module ActiveRecord binds) end - def substitute_values(values) + def substitute_values(values) # :nodoc: substitutes = values.sort_by { |arel_attr,_| arel_attr.name } binds = substitutes.map do |arel_attr, value| [@klass.columns_hash[arel_attr.name], value] -- cgit v1.2.3 From ebd7cc6f459e43aa03a6b8095266888909e0ee4d Mon Sep 17 00:00:00 2001 From: Chris Thompson Date: Thu, 7 Mar 2013 14:43:00 -0700 Subject: Fix #8856 Ensure has_one association=(associate) triggers save. activerecord/lib/active_record/associations.rb states: # [association=(associate)] # Assigns the associate object, extracts the primary key, sets it as the foreign key, # and saves the associate object. Since commit 42dd5d9f2976677a4bf22347f2dde1a8135dfbb4 to fix #7191, this is no longer the case if the associate has changed, but is the same object. For example: # Pirate has_one :ship pirate = Pirate.create!(catchphrase: "A Pirate") ship = pirate.build_ship(name: 'old name') ship.save! ship.name = 'new name' pirate.ship = ship That last line should trigger a save. Although we are not changing the association, the associate (ship) has changed. --- activerecord/lib/active_record/associations/has_one_association.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 98bd010f70..920038a543 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -25,9 +25,8 @@ module ActiveRecord raise_on_type_mismatch!(record) if record load_target - # If target and record are nil, or target is equal to record, - # we don't need to have transaction. - if (target || record) && target != record + return self.target if !(target || record) + if (target != record) || record.changed? transaction_if(save) do remove_target!(options[:dependent]) if target && !target.destroyed? -- cgit v1.2.3 From 2496bd9a98a817dbee567948e19fbee08991d347 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Tue, 30 Apr 2013 17:24:29 -0700 Subject: Mute psql output when running rake db:schema:load --- activerecord/lib/active_record/tasks/postgresql_database_tasks.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb index 0b1b030516..4413330fab 100644 --- a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb @@ -59,7 +59,7 @@ module ActiveRecord def structure_load(filename) set_psql_env - Kernel.system("psql -f #{filename} #{configuration['database']}") + Kernel.system("psql -q -f #{filename} #{configuration['database']}") end private -- cgit v1.2.3 From 78db16d440c610edeb8b2bb37233a5991caf4a29 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 30 Apr 2013 18:38:57 -0700 Subject: maintain return value for recreate_database --- .../lib/active_record/connection_adapters/abstract_mysql_adapter.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 2a7b855f95..d098ded77c 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -364,8 +364,9 @@ module ActiveRecord # and creates it again using the provided +options+. def recreate_database(name, options = {}) drop_database(name) - create_database(name, options) + sql = create_database(name, options) reconnect! + sql end # Create a new MySQL database with optional :charset and :collation. -- cgit v1.2.3 From 55c40c0ecec50936c439548b440216c62aa4ccbb Mon Sep 17 00:00:00 2001 From: Chad Moone Date: Thu, 25 Apr 2013 00:46:40 -0400 Subject: allow override of uuid_generate_v4() default by passing default: nil without this, it's not possible to use UUID primary keys without uuid-ossp installed and activated --- .../connection_adapters/postgresql_adapter.rb | 31 +++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index bf403c3ae0..6040eeed00 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -330,9 +330,38 @@ module ActiveRecord class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition include ColumnMethods + # Defines the primary key field. + # Use of the native PostgreSQL UUID type is supported, and can be used + # by defining your tables as such: + # + # create_table :stuffs, id: :uuid do |t| + # t.string :content + # t.timestamps + # end + # + # By default, this will use the +uuid_generate_v4()+ function from the + # +uuid-ossp+ extension, which MUST be enabled on your databse. To enable + # the +uuid-ossp+ extension, you can use the +enable_extension+ method in your + # migrations To use a UUID primary key without +uuid-ossp+ enabled, you can + # set the +:default+ option to nil: + # + # create_table :stuffs, id: false do |t| + # t.primary_key :id, :uuid, default: nil + # t.uuid :foo_id + # t.timestamps + # end + # + # You may also pass a different UUID generation function from +uuid-ossp+ + # or another library. + # + # Note that setting the UUID primary key default value to +nil+ + # will require you to assure that you always provide a UUID value + # before saving a record (as primary keys cannot be nil). This might be + # done via the SecureRandom.uuid method and a +before_save+ callback, + # for instance. def primary_key(name, type = :primary_key, options = {}) return super unless type == :uuid - options[:default] ||= 'uuid_generate_v4()' + options[:default] = options.fetch(:default, 'uuid_generate_v4()') options[:primary_key] = true column name, type, options end -- cgit v1.2.3 From 54122067acaad39b277a5363c6d11d6804c7bf6b Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Wed, 1 May 2013 11:33:11 -0700 Subject: Handle aliased attributes in ActiveRecord::Relation. When using symbol keys, ActiveRecord will now translate aliased attribute names to the actual column name used in the database: With the model class Topic alias_attribute :heading, :title end The call Topic.where(heading: 'The First Topic') should yield the same result as Topic.where(title: 'The First Topic') This also applies to ActiveRecord::Relation::Calculations calls such as `Model.sum(:aliased)` and `Model.pluck(:aliased)`. This will not work with SQL fragment strings like `Model.sum('DISTINCT aliased')`. Github #7839 *Godfrey Chan* --- .../lib/active_record/relation/calculations.rb | 26 +++++++++++++++------- .../active_record/relation/predicate_builder.rb | 4 ++++ 2 files changed, 22 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 64e1ff9a6a..7239270c4d 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -27,7 +27,7 @@ module ActiveRecord # Calculates the average value on a given column. Returns +nil+ if there's # no row. See +calculate+ for examples with options. # - # Person.average('age') # => 35.8 + # Person.average(:age) # => 35.8 def average(column_name, options = {}) calculate(:average, column_name, options) end @@ -36,7 +36,7 @@ module ActiveRecord # with the same data type of the column, or +nil+ if there's no row. See # +calculate+ for examples with options. # - # Person.minimum('age') # => 7 + # Person.minimum(:age) # => 7 def minimum(column_name, options = {}) calculate(:minimum, column_name, options) end @@ -45,7 +45,7 @@ module ActiveRecord # with the same data type of the column, or +nil+ if there's no row. See # +calculate+ for examples with options. # - # Person.maximum('age') # => 93 + # Person.maximum(:age) # => 93 def maximum(column_name, options = {}) calculate(:maximum, column_name, options) end @@ -54,7 +54,7 @@ module ActiveRecord # with the same data type of the column, 0 if there's no row. See # +calculate+ for examples with options. # - # Person.sum('age') # => 4562 + # Person.sum(:age) # => 4562 def sum(*args) if block_given? ActiveSupport::Deprecation.warn( @@ -101,6 +101,10 @@ module ActiveRecord def calculate(operation, column_name, options = {}) relation = with_default_scope + if column_name.is_a?(Symbol) && attribute_aliases.key?(column_name.to_s) + column_name = attribute_aliases[column_name.to_s].to_sym + end + if relation.equal?(self) if has_include?(column_name) construct_relation_for_association_calculations.calculate(operation, column_name, options) @@ -149,11 +153,17 @@ module ActiveRecord # def pluck(*column_names) column_names.map! do |column_name| - if column_name.is_a?(Symbol) && self.column_names.include?(column_name.to_s) - "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}" - else - column_name + if column_name.is_a?(Symbol) + if attribute_aliases.key?(column_name.to_s) + column_name = attribute_aliases[column_name.to_s].to_sym + end + + if self.columns_hash.key?(column_name.to_s) + column_name = "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}" + end end + + column_name end if has_include?(column_names.first) diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index f44d46d15b..b7609c97b5 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -6,6 +6,10 @@ module ActiveRecord attributes.each do |column, value| table = default_table + if column.is_a?(Symbol) && klass.attribute_aliases.key?(column.to_s) + column = klass.attribute_aliases[column.to_s] + end + if value.is_a?(Hash) if value.empty? queries << '1=0' -- cgit v1.2.3 From 14a75a54a802b35db5a7802eca90dae9aa8e3518 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 1 May 2013 21:24:43 -0300 Subject: Improve docs for postgresql with uuid primary keys [ci skip] Introduced in 09ac1776abc0d3482f491f2d49f47bcb3d9a4ad7. --- .../connection_adapters/postgresql_adapter.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 6040eeed00..654aac8453 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -340,10 +340,10 @@ module ActiveRecord # end # # By default, this will use the +uuid_generate_v4()+ function from the - # +uuid-ossp+ extension, which MUST be enabled on your databse. To enable + # +uuid-ossp+ extension, which MUST be enabled on your database. To enable # the +uuid-ossp+ extension, you can use the +enable_extension+ method in your - # migrations To use a UUID primary key without +uuid-ossp+ enabled, you can - # set the +:default+ option to nil: + # migrations. To use a UUID primary key without +uuid-ossp+ enabled, you can + # set the +:default+ option to +nil+: # # create_table :stuffs, id: false do |t| # t.primary_key :id, :uuid, default: nil @@ -354,11 +354,10 @@ module ActiveRecord # You may also pass a different UUID generation function from +uuid-ossp+ # or another library. # - # Note that setting the UUID primary key default value to +nil+ - # will require you to assure that you always provide a UUID value - # before saving a record (as primary keys cannot be nil). This might be - # done via the SecureRandom.uuid method and a +before_save+ callback, - # for instance. + # Note that setting the UUID primary key default value to +nil+ will + # require you to assure that you always provide a UUID value before saving + # a record (as primary keys cannot be +nil+). This might be done via the + # +SecureRandom.uuid+ method and a +before_save+ callback, for instance. def primary_key(name, type = :primary_key, options = {}) return super unless type == :uuid options[:default] = options.fetch(:default, 'uuid_generate_v4()') -- cgit v1.2.3 From 483c301e0a132a6415355be2296fb932d07bd1c5 Mon Sep 17 00:00:00 2001 From: Johnny Holton Date: Wed, 10 Apr 2013 23:29:19 -0400 Subject: destroys association records before saving/inserting new association records fixes bug introduced by #3329 These are the conditions necessary to reproduce the bug: - For an association, autosave => true. - An association record is being destroyed - A new association record is being created. - There is a unique index one of the association's fields. - The record being created has the same value as the record being destroyed on the indexed field. Before, the deletion of records was postponed until after all insertions/saves. Therefore the new record with the identical value in the indexed field caused a non-unique value error to be thrown at the database level. With this fix, the deletions happen first, before the insertions/saves. Therefore the record with the duplicate value is gone from the database before the new record is created, thereby avoiding the non-uniuqe value error. --- activerecord/lib/active_record/autosave_association.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index b0bd78ad46..87d4daa6d9 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -335,15 +335,18 @@ module ActiveRecord autosave = reflection.options[:autosave] if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave) - records_to_destroy = [] + + if autosave + records_to_destroy = records.select(&:marked_for_destruction?) + records_to_destroy.each { |record| association.destroy(record) } + records -= records_to_destroy + end + records.each do |record| - next if record.destroyed? saved = true - if autosave && record.marked_for_destruction? - records_to_destroy << record - elsif autosave != false && (@new_record_before_save || record.new_record?) + if autosave != false && (@new_record_before_save || record.new_record?) if autosave saved = association.insert_record(record, false) else @@ -355,10 +358,6 @@ module ActiveRecord raise ActiveRecord::Rollback unless saved end - - records_to_destroy.each do |record| - association.destroy(record) - end end # reconstruct the scope now that we know the owner's id -- cgit v1.2.3 From 56445c9075e22fdf00b534363c0744c78d6037f0 Mon Sep 17 00:00:00 2001 From: markevich Date: Thu, 2 May 2013 18:09:59 +0300 Subject: rake:db:test:prepare falls back to original environment after execution. --- activerecord/lib/active_record/railties/databases.rake | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 92bef09ff5..434af3c5f8 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -319,9 +319,13 @@ db_namespace = namespace :db do # desc "Recreate the test database from an existent schema.rb file" task :load_schema => 'db:test:purge' do - ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test']) - ActiveRecord::Schema.verbose = false - db_namespace["schema:load"].invoke + begin + ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test']) + ActiveRecord::Schema.verbose = false + db_namespace["schema:load"].invoke + ensure + ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Rails.env]) + end end # desc "Recreate the test database from an existent structure.sql file" -- cgit v1.2.3 From 950ef055852fce3c7dca7fa1c5cc3eb3f9c93265 Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Thu, 2 May 2013 22:28:33 +0200 Subject: Add parameter :sslcompression to PostgreSQL adapter. It is new in PostgreSQL-9.2 . --- .../lib/active_record/connection_adapters/postgresql_adapter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 654aac8453..df729259e9 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -20,8 +20,8 @@ module ActiveRecord VALID_CONN_PARAMS = [:host, :hostaddr, :port, :dbname, :user, :password, :connect_timeout, :client_encoding, :options, :application_name, :fallback_application_name, :keepalives, :keepalives_idle, :keepalives_interval, :keepalives_count, - :tty, :sslmode, :requiressl, :sslcert, :sslkey, :sslrootcert, :sslcrl, - :requirepeer, :krbsrvname, :gsslib, :service] + :tty, :sslmode, :requiressl, :sslcompression, :sslcert, :sslkey, + :sslrootcert, :sslcrl, :requirepeer, :krbsrvname, :gsslib, :service] # Establishes a connection to the database that's used by all Active Record objects def postgresql_connection(config) -- cgit v1.2.3 From b46e6416dc6607545e2dea92e6788986b261357c Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Fri, 3 May 2013 01:00:43 -0400 Subject: added to rdoc for unscope that default_scope wins --- activerecord/lib/active_record/relation/query_methods.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 9fcd2d06c5..d020f1ba52 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -340,6 +340,9 @@ module ActiveRecord # User.where(name: "John", active: true).unscope(where: :name) # == User.where(active: true) # + # This method is applied before the default_scope is applied. So the conditions + # specified in default_scope will not be removed. + # # Note that this method is more generalized than ActiveRecord::SpawnMethods#except # because #except will only affect a particular relation's values. It won't wipe # the order, grouping, etc. when that relation is merged. For example: -- cgit v1.2.3 From 534030cf83b078b10c08ffb587cc56e86773ea8c Mon Sep 17 00:00:00 2001 From: Olek Janiszewski Date: Tue, 26 Feb 2013 03:06:35 +0100 Subject: Do not overwrite manually built records during one-to-one nested attribute assignment For one-to-one nested associations, if you build the new (in-memory) child object yourself before assignment, then the NestedAttributes module will not overwrite it, e.g.: class Member < ActiveRecord::Base has_one :avatar accepts_nested_attributes_for :avatar def avatar super || build_avatar(width: 200) end end member = Member.new member.avatar_attributes = {icon: 'sad'} member.avatar.width # => 200 --- .../lib/active_record/associations/association.rb | 12 ++++--- .../lib/active_record/nested_attributes.rb | 39 ++++++++++++++++++---- 2 files changed, 40 insertions(+), 11 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index db0553ea76..710babe024 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -164,6 +164,13 @@ module ActiveRecord @reflection = @owner.class.reflect_on_association(reflection_name) end + def initialize_attributes(record) #:nodoc: + skip_assign = [reflection.foreign_key, reflection.type].compact + attributes = create_scope.except(*(record.changed - skip_assign)) + record.assign_attributes(attributes) + set_inverse_instance(record) + end + private def find_target? @@ -233,10 +240,7 @@ module ActiveRecord def build_record(attributes) reflection.build_association(attributes) do |record| - skip_assign = [reflection.foreign_key, reflection.type].compact - attributes = create_scope.except(*(record.changed - skip_assign)) - record.assign_attributes(attributes) - set_inverse_instance(record) + initialize_attributes(record) end end end diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index d607f49e2b..021832de46 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -229,6 +229,23 @@ module ActiveRecord # belongs_to :member, inverse_of: :posts # validates_presence_of :member # end + # + # For one-to-one nested associations, if you build the new (in-memory) + # child object yourself before assignment, then this module will not + # overwrite it, e.g.: + # + # class Member < ActiveRecord::Base + # has_one :avatar + # accepts_nested_attributes_for :avatar + # + # def avatar + # super || build_avatar(width: 200) + # end + # end + # + # member = Member.new + # member.avatar_attributes = {icon: 'sad'} + # member.avatar.width # => 200 module ClassMethods REJECT_ALL_BLANK_PROC = proc { |attributes| attributes.all? { |key, value| key == '_destroy' || value.blank? } } @@ -356,20 +373,28 @@ module ActiveRecord def assign_nested_attributes_for_one_to_one_association(association_name, attributes) options = self.nested_attributes_options[association_name] attributes = attributes.with_indifferent_access + existing_record = send(association_name) - if (options[:update_only] || !attributes['id'].blank?) && (record = send(association_name)) && - (options[:update_only] || record.id.to_s == attributes['id'].to_s) - assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy]) unless call_reject_if(association_name, attributes) + if (options[:update_only] || !attributes['id'].blank?) && existing_record && + (options[:update_only] || existing_record.id.to_s == attributes['id'].to_s) + assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy]) unless call_reject_if(association_name, attributes) elsif attributes['id'].present? raise_nested_attributes_record_not_found!(association_name, attributes['id']) elsif !reject_new_record?(association_name, attributes) - method = "build_#{association_name}" - if respond_to?(method) - send(method, attributes.except(*UNASSIGNABLE_KEYS)) + assignable_attributes = attributes.except(*UNASSIGNABLE_KEYS) + + if existing_record && existing_record.new_record? + existing_record.assign_attributes(assignable_attributes) + association(association_name).initialize_attributes(existing_record) else - raise ArgumentError, "Cannot build association `#{association_name}'. Are you trying to build a polymorphic one-to-one association?" + method = "build_#{association_name}" + if respond_to?(method) + send(method, assignable_attributes) + else + raise ArgumentError, "Cannot build association `#{association_name}'. Are you trying to build a polymorphic one-to-one association?" + end end end end -- cgit v1.2.3 From fa87e3166fd404f9d494965fb20712a55af078fb Mon Sep 17 00:00:00 2001 From: Brian Buchanan Date: Fri, 3 May 2013 16:05:55 -0700 Subject: Make SchemaDumper emit "id: :uuid" when appropriate. Fixes #10451. --- .../connection_adapters/postgresql/schema_statements.rb | 3 ++- activerecord/lib/active_record/schema_dumper.rb | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index d9b807bba4..98916b06a5 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -321,6 +321,7 @@ module ActiveRecord result = query(<<-end_sql, 'SCHEMA')[0] SELECT attr.attname, CASE + WHEN pg_get_expr(def.adbin, def.adrelid) !~* 'nextval' THEN NULL WHEN split_part(pg_get_expr(def.adbin, def.adrelid), '''', 2) ~ '.' THEN substr(split_part(pg_get_expr(def.adbin, def.adrelid), '''', 2), strpos(split_part(pg_get_expr(def.adbin, def.adrelid), '''', 2), '.')+1) @@ -332,7 +333,7 @@ module ActiveRecord JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1]) WHERE t.oid = '#{quote_table_name(table)}'::regclass AND cons.contype = 'p' - AND pg_get_expr(def.adbin, def.adrelid) ~* 'nextval' + AND pg_get_expr(def.adbin, def.adrelid) ~* 'nextval|uuid_generate' end_sql end diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 10c6d272cd..1181cc739e 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -106,9 +106,12 @@ HEADER end tbl.print " create_table #{remove_prefix_and_suffix(table).inspect}" - if columns.detect { |c| c.name == pk } + pkcol = columns.detect { |c| c.name == pk } + if pkcol if pk != 'id' tbl.print %Q(, primary_key: "#{pk}") + elsif pkcol.sql_type == 'uuid' + tbl.print ", id: :uuid" end else tbl.print ", id: false" -- cgit v1.2.3 From 20549051f899c8f85cd3592d1ad6490a951392e2 Mon Sep 17 00:00:00 2001 From: Akshay Khole Date: Sun, 5 May 2013 00:25:22 +0530 Subject: Changing method call according to coding conventions --- .../lib/active_record/connection_adapters/postgresql_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index df729259e9..88b09e7999 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -768,7 +768,7 @@ module ActiveRecord end def exec_cache(sql, binds) - stmt_key = prepare_statement sql + stmt_key = prepare_statement(sql) # Clear the queue @connection.get_last_result -- cgit v1.2.3 From 3771e4d51122e1ec22728029bae00f121d5d4e3b Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Fri, 3 May 2013 00:46:18 -0400 Subject: raise IrreversibleMigration if no column given fixes #10419 Following code should raise IrreversibleMigration. But the code was failing since options is an array and not a hash. def change change_table :users do |t| t.remove_index [:name, :email] end end Fix was to check if the options is a Hash before operating on it. --- activerecord/lib/active_record/migration/command_recorder.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb index 79c55045ba..9782a48055 100644 --- a/activerecord/lib/active_record/migration/command_recorder.rb +++ b/activerecord/lib/active_record/migration/command_recorder.rb @@ -144,7 +144,10 @@ module ActiveRecord def invert_remove_index(args) table, options = *args - raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option." unless options && options[:column] + + unless options && options.is_a?(Hash) && options[:column] + raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option." + end options = options.dup [:add_index, [table, options.delete(:column), options]] -- cgit v1.2.3 From a6bc35c82cd58aac61608391f38fda4e034be0f7 Mon Sep 17 00:00:00 2001 From: Zach Ohlgren Date: Thu, 25 Apr 2013 17:20:33 -0700 Subject: Fix bug in ActiveRecord::Sanitization#sanitize_sql_hash_for_conditions Fixing CHANGLOG description Remove extra line. Remove blank lines. --- activerecord/lib/active_record/sanitization.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/sanitization.rb b/activerecord/lib/active_record/sanitization.rb index 3c5b871e99..0ed97b66d6 100644 --- a/activerecord/lib/active_record/sanitization.rb +++ b/activerecord/lib/active_record/sanitization.rb @@ -89,7 +89,7 @@ module ActiveRecord attrs = expand_hash_conditions_for_aggregates(attrs) table = Arel::Table.new(table_name, arel_engine).alias(default_table_name) - PredicateBuilder.build_from_hash(self.class, attrs, table).map { |b| + PredicateBuilder.build_from_hash(self, attrs, table).map { |b| connection.visitor.accept b }.join(' AND ') end -- cgit v1.2.3 From 228720ef19e7dcf7c21f4ef2171906cc7c8c97f1 Mon Sep 17 00:00:00 2001 From: Ben Tucker Date: Mon, 6 May 2013 18:31:20 -0400 Subject: Confirm a record has not already been destroyed before decrementing counter cache At present, calling destroy multiple times on the same record results in the belongs_to counter cache being decremented multiple times. With this change the record is checked for whether it is already destroyed prior to decrementing the counter cache. --- activerecord/lib/active_record/associations/builder/belongs_to.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index 543a0247d1..63e9526436 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -34,7 +34,9 @@ module ActiveRecord::Associations::Builder def belongs_to_counter_cache_before_destroy_for_#{name} unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == #{foreign_key.to_sym.inspect} record = #{name} - record.class.decrement_counter(:#{cache_column}, record.id) unless record.nil? + if record && !self.destroyed? + record.class.decrement_counter(:#{cache_column}, record.id) + end end end -- cgit v1.2.3 From 0e00c6b296b48e35fc3997648561f5da7295098a Mon Sep 17 00:00:00 2001 From: Patrick Robertson Date: Tue, 7 May 2013 08:21:41 -0400 Subject: Handle other pk types in PostgreSQL gracefully. In #10410 it was noted that you can no longer create PK's with the type of bigserial in PostgreSQL in 4.0.0.rc1. This is mostly because the newer adapter is checking for column type with the id column instead of just letting it pass through like it did before. Side effects: You may just create a PK column of a type that you really don't want to be your PK. As far as I can tell this was allowed in 3.2.X and perhaps an exception should be raised if you try and do something extremely dumb. --- .../lib/active_record/connection_adapters/postgresql_adapter.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 88b09e7999..2d0eef6c84 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -359,8 +359,12 @@ module ActiveRecord # a record (as primary keys cannot be +nil+). This might be done via the # +SecureRandom.uuid+ method and a +before_save+ callback, for instance. def primary_key(name, type = :primary_key, options = {}) - return super unless type == :uuid - options[:default] = options.fetch(:default, 'uuid_generate_v4()') + return super unless type = :primary_key + + if type == :uuid + options[:default] = options.fetch(:default, 'uuid_generate_v4()') + end + options[:primary_key] = true column name, type, options end -- cgit v1.2.3 From 2b5e4f7f983a891bfcf288fba386cc534e64223e Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 7 May 2013 10:39:41 -0700 Subject: Revert "Merge pull request #10455 from patricksrobertson/bigserial_id_not_identifying_pk" This reverts commit 3043d45eefc3776d5f3a9e7d212a01f99d869ef8, reversing changes made to ca0275d36b395631725c4583db5a45c06443fdb9. --- .../lib/active_record/connection_adapters/postgresql_adapter.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 2d0eef6c84..88b09e7999 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -359,12 +359,8 @@ module ActiveRecord # a record (as primary keys cannot be +nil+). This might be done via the # +SecureRandom.uuid+ method and a +before_save+ callback, for instance. def primary_key(name, type = :primary_key, options = {}) - return super unless type = :primary_key - - if type == :uuid - options[:default] = options.fetch(:default, 'uuid_generate_v4()') - end - + return super unless type == :uuid + options[:default] = options.fetch(:default, 'uuid_generate_v4()') options[:primary_key] = true column name, type, options end -- cgit v1.2.3 From 26d19b4661f3d89a075b5f05d926c578ff0c730f Mon Sep 17 00:00:00 2001 From: wangjohn Date: Fri, 1 Mar 2013 16:49:33 -0500 Subject: Created a method to automatically find inverse associations and cache the results. Added tests to check to make sure that inverse associations are automatically found when has_many, has_one, or belongs_to associations are defined. --- .../active_record/associations/builder/has_many.rb | 2 +- .../associations/builder/singular_association.rb | 2 +- .../lib/active_record/nested_attributes.rb | 5 + activerecord/lib/active_record/reflection.rb | 101 ++++++++++++++++++++- 4 files changed, 105 insertions(+), 5 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/builder/has_many.rb b/activerecord/lib/active_record/associations/builder/has_many.rb index 0d1bdd21ee..429def5455 100644 --- a/activerecord/lib/active_record/associations/builder/has_many.rb +++ b/activerecord/lib/active_record/associations/builder/has_many.rb @@ -5,7 +5,7 @@ module ActiveRecord::Associations::Builder end def valid_options - super + [:primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache] + super + [:primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :automatic_inverse_of, :counter_cache] end def valid_dependent_options diff --git a/activerecord/lib/active_record/associations/builder/singular_association.rb b/activerecord/lib/active_record/associations/builder/singular_association.rb index 6a5830e57f..f06426a09d 100644 --- a/activerecord/lib/active_record/associations/builder/singular_association.rb +++ b/activerecord/lib/active_record/associations/builder/singular_association.rb @@ -1,7 +1,7 @@ module ActiveRecord::Associations::Builder class SingularAssociation < Association #:nodoc: def valid_options - super + [:remote, :dependent, :counter_cache, :primary_key, :inverse_of] + super + [:remote, :dependent, :counter_cache, :primary_key, :inverse_of, :automatic_inverse_of] end def constructable? diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index 021832de46..8bdaeef924 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -305,6 +305,11 @@ module ActiveRecord reflection.options[:autosave] = true add_autosave_association_callbacks(reflection) + # Clear cached values of any inverse associations found in the + # reflection and prevent the reflection from finding inverses + # automatically in the future. + reflection.remove_automatic_inverse_of! + nested_attributes_options = self.nested_attributes_options.dup nested_attributes_options[association_name.to_sym] = options self.nested_attributes_options = nested_attributes_options diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 60eda96f08..0ba860a186 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -181,6 +181,7 @@ module ActiveRecord def initialize(*args) super @collection = [:has_many, :has_and_belongs_to_many].include?(macro) + @automatic_inverse_of = nil end # Returns a new, unsaved instance of the associated class. +attributes+ will @@ -289,15 +290,32 @@ module ActiveRecord alias :source_macro :macro def has_inverse? - @options[:inverse_of] + @options[:inverse_of] || find_inverse_of_automatically end def inverse_of - if has_inverse? - @inverse_of ||= klass.reflect_on_association(options[:inverse_of]) + @inverse_of ||= if options[:inverse_of] + klass.reflect_on_association(options[:inverse_of]) + else + find_inverse_of_automatically end end + # Clears the cached value of +@inverse_of+ on this object. This will + # not remove the :inverse_of option however, so future calls on the + # +inverse_of+ will have to recompute the inverse. + def clear_inverse_of_cache! + @inverse_of = nil + end + + # Removes the cached inverse association that was found automatically + # and prevents this object from finding the inverse association + # automatically in the future. + def remove_automatic_inverse_of! + @automatic_inverse_of = nil + options[:automatic_inverse_of] = false + end + def polymorphic_inverse_of(associated_class) if has_inverse? if inverse_relationship = associated_class.reflect_on_association(options[:inverse_of]) @@ -366,7 +384,84 @@ module ActiveRecord options.key? :polymorphic end + VALID_AUTOMATIC_INVERSE_MACROS = [:has_many, :has_one, :belongs_to] + INVALID_AUTOMATIC_INVERSE_OPTIONS = [:conditions, :through, :polymorphic, :foreign_key] + private + # Attempts to find the inverse association automatically. + # If it cannot find a suitable inverse association, it returns + # nil. + def find_inverse_of_automatically + if @automatic_inverse_of == false + nil + elsif @automatic_inverse_of.nil? + set_automatic_inverse_of + else + klass.reflect_on_association(@automatic_inverse_of) + end + end + + # Sets the +@automatic_inverse_of+ instance variable, and returns + # either nil or the inverse association that it finds. + # + # This method caches the inverse association that is found so that + # future calls to +find_inverse_of_automatically+ have much less + # overhead. + def set_automatic_inverse_of + if can_find_inverse_of_automatically?(self) + inverse_name = active_record.name.downcase.to_sym + + begin + reflection = klass.reflect_on_association(inverse_name) + rescue NameError + # Give up: we couldn't compute the klass type so we won't be able + # to find any associations either. + reflection = false + end + + if valid_inverse_reflection?(reflection) + @automatic_inverse_of = inverse_name + reflection + else + @automatic_inverse_of = false + nil + end + else + @automatic_inverse_of = false + nil + end + end + + # Checks if the inverse reflection that is returned from the + # +set_automatic_inverse_of+ method is a valid reflection. We must + # make sure that the reflection's active_record name matches up + # with the current reflection's klass name. + # + # Note: klass will always be valid because when there's a NameError + # from calling +klass+, +reflection+ will already be set to false. + def valid_inverse_reflection?(reflection) + reflection && + klass.name == reflection.active_record.try(:name) && + klass.primary_key == reflection.active_record_primary_key && + can_find_inverse_of_automatically?(reflection) + end + + # Checks to see if the reflection doesn't have any options that prevent + # us from being able to guess the inverse automatically. First, the + # +automatic_inverse_of+ option cannot be set to false. Second, we must + # have :has_many, :has_one, :belongs_to associations. Third, we must + # not have options such as :class_name or :polymorphic which prevent us + # from correctly guessing the inverse association. + # + # Anything with a scope can additionally ruin our attempt at finding an + # inverse, so we exclude reflections with scopes. + def can_find_inverse_of_automatically?(reflection) + reflection.options[:automatic_inverse_of] != false && + VALID_AUTOMATIC_INVERSE_MACROS.include?(reflection.macro) && + !INVALID_AUTOMATIC_INVERSE_OPTIONS.any? { |opt| reflection.options[opt] } && + !reflection.scope + end + def derive_class_name class_name = name.to_s.camelize class_name = class_name.singularize if collection? -- cgit v1.2.3 From fdba949b47154f43aa8d0cce5177a75080f47836 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 8 May 2013 14:20:06 -0400 Subject: extracted piece of code into a method In order to fix #10421 I need to enable merge to take an option so that relations could be merged without making the last where condition to win. That fix would forever reside in 4-0-stable branch and would not be merged to master since using scope without lambda has been deprecated. In this commit I have extracted code into a method and I think it makes code look better. Hence the request to merge it in both master and 4-0-stable. If there is any concern then this code can be merged only in 4-0-stable and that would be fine too. --- activerecord/lib/active_record/relation/merger.rb | 25 +++++++++++------------ 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/merger.rb b/activerecord/lib/active_record/relation/merger.rb index 936b83261e..bda7a76330 100644 --- a/activerecord/lib/active_record/relation/merger.rb +++ b/activerecord/lib/active_record/relation/merger.rb @@ -139,21 +139,20 @@ module ActiveRecord if values[:where].empty? || relation.where_values.empty? relation.where_values + values[:where] else - # Remove equalities from the existing relation with a LHS which is - # present in the relation being merged in. + sanitized_wheres + values[:where] + end + end - seen = Set.new - values[:where].each { |w| - if w.respond_to?(:operator) && w.operator == :== - seen << w.left - end - } + # Remove equalities from the existing relation with a LHS which is + # present in the relation being merged in. + def sanitized_wheres + seen = Set.new + values[:where].each do |w| + seen << w.left if w.respond_to?(:operator) && w.operator == :== + end - relation.where_values.reject { |w| - w.respond_to?(:operator) && - w.operator == :== && - seen.include?(w.left) - } + values[:where] + relation.where_values.reject do |w| + w.respond_to?(:operator) && w.operator == :== && seen.include?(w.left) end end end -- cgit v1.2.3 From d9490631d2e5cf99d748541bd9c3c43adcdd6e6c Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Thu, 9 May 2013 02:06:03 -0400 Subject: minor rdoc cleanup for reflection methods --- activerecord/lib/active_record/reflection.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 60eda96f08..d26fb14413 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -398,7 +398,7 @@ module ActiveRecord delegate :foreign_key, :foreign_type, :association_foreign_key, :active_record_primary_key, :type, :to => :source_reflection - # Gets the source of the through reflection. It checks both a singularized + # Returns the source of the through reflection. It checks both a singularized # and pluralized form for :belongs_to or :has_many. # # class Post < ActiveRecord::Base @@ -412,8 +412,7 @@ module ActiveRecord # end # # tags_reflection = Post.reflect_on_association(:tags) - # - # taggings_reflection = tags_reflection.source_reflection + # tags_reflection.source_reflection # # => # def source_reflection @@ -429,7 +428,8 @@ module ActiveRecord # end # # tags_reflection = Post.reflect_on_association(:tags) - # taggings_reflection = tags_reflection.through_reflection + # tags_reflection.through_reflection + # # => # def through_reflection @through_reflection ||= active_record.reflect_on_association(options[:through]) -- cgit v1.2.3 From fe7cf89193bcbb92d1a4dd86e67da9627c20a167 Mon Sep 17 00:00:00 2001 From: aditya-kapoor Date: Thu, 9 May 2013 23:50:05 +0530 Subject: Added documentation for ActiveRecord::Base#next_migration_number --- activerecord/lib/active_record/migration.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 6c020e1d57..4a04748680 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -668,6 +668,11 @@ module ActiveRecord copied end + # Determines the version number of the next migration + # if the timestamped migrations are activated then the comparison with the current time is made + # and then higer of the two values is selected + # For non timestamped values, the simple numbers are used in the format of "057", "570" + def next_migration_number(number) if ActiveRecord::Base.timestamped_migrations [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % number].max -- cgit v1.2.3 From d7abe91cc73a8991033042f4cb7467bba7fa2339 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 25 Apr 2013 21:37:44 +0100 Subject: Set the inverse when association queries are refined Suppose Man has_many interests, and inverse_of is used. Man.first.interests.first.man will correctly execute two queries, avoiding the need for a third query when Interest#man is called. This is because CollectionAssociation#first calls set_inverse_instance. However Man.first.interests.where("1=1").first.man will execute three queries, even though this is obviously a subset of the records in the association. This is because calling where("1=1") spawns a new Relation object from the CollectionProxy object, and the Relation has no knowledge of the association, so it cannot set the inverse instance. This commit solves the problem by making relations spawned from CollectionProxies return a new Relation subclass called AssociationRelation, which does know about associations. Records loaded from this class will get the inverse instance set properly. Fixes #5717. Live commit from La Conf! :sparkles: --- activerecord/lib/active_record/association_relation.rb | 14 ++++++++++++++ activerecord/lib/active_record/associations/association.rb | 6 +++++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 activerecord/lib/active_record/association_relation.rb (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/association_relation.rb b/activerecord/lib/active_record/association_relation.rb new file mode 100644 index 0000000000..e2976577f6 --- /dev/null +++ b/activerecord/lib/active_record/association_relation.rb @@ -0,0 +1,14 @@ +module ActiveRecord + class AssociationRelation < Relation + def initialize(klass, table, association) + super(klass, table) + @association = association + end + + private + + def exec_queries + super.each { |r| @association.set_inverse_instance r } + end + end +end diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 710babe024..608a6af16c 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -122,7 +122,11 @@ module ActiveRecord # Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the # through association's scope) def target_scope - klass.all + all = klass.all + scope = AssociationRelation.new(klass, klass.arel_table, self) + scope.merge! all + scope.default_scoped = all.default_scoped? + scope end # Loads the \target if needed and returns it. -- cgit v1.2.3 From 15d6e4dce7126fe24bce5cdb91d2ffee68648420 Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Mon, 28 May 2012 12:23:37 -0700 Subject: Fix that #exists? can produce invalid SQL: "SELECT DISTINCT DISTINCT" The combination of a :uniq => true association and the #distinct call in #construct_limited_ids_condition combine to create invalid SQL, because we're explicitly selecting DISTINCT, and also sending #distinct on to AREL, via the relation#distinct_value. Rather than build a select distinct clause in #construct_limited_ids_condition, I set #distinct! and pass just the columns into the select statement. This requires introducing a #columns_for_distinct method to return the select columns but not the statement itself. --- .../connection_adapters/abstract/schema_statements.rb | 14 +++++++++++--- .../connection_adapters/postgresql/schema_statements.rb | 9 ++------- activerecord/lib/active_record/relation/finder_methods.rb | 4 ++-- 3 files changed, 15 insertions(+), 12 deletions(-) (limited to 'activerecord/lib/active_record') 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 9c0c4e3ef0..6e1f43cce6 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -706,12 +706,20 @@ module ActiveRecord end # SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause. - # Both PostgreSQL and Oracle overrides this for custom DISTINCT syntax. # - # distinct("posts.id", "posts.created_at desc") + # distinct("posts.id", ["posts.created_at desc"]) # def distinct(columns, order_by) - "DISTINCT #{columns}" + "DISTINCT #{columns_for_distinct(columns, order_by)}" + end + + # Given a set of columns and an ORDER BY clause, returns the columns for a SELECT DISTINCT. + # Both PostgreSQL and Oracle overrides this for custom DISTINCT syntax - they + # require the order columns appear in the SELECT. + # + # columns_for_distinct("posts.id", ["posts.created_at desc"]) + def columns_for_distinct(columns, orders) + columns end # Adds timestamps (+created_at+ and +updated_at+) columns to the named table. diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index 98916b06a5..8feee23df0 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -467,14 +467,9 @@ module ActiveRecord end end - # Returns a SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause. - # # PostgreSQL requires the ORDER BY columns in the select list for distinct queries, and # requires that the ORDER BY include the distinct column. - # - # distinct("posts.id", ["posts.created_at desc"]) - # # => "DISTINCT posts.id, posts.created_at AS alias_0" - def distinct(columns, orders) #:nodoc: + def columns_for_distinct(columns, orders) #:nodoc: order_columns = orders.map{ |s| # Convert Arel node to string s = s.to_sql unless s.is_a?(String) @@ -482,7 +477,7 @@ module ActiveRecord s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '') }.reject(&:blank?).map.with_index { |column, i| "#{column} AS alias_#{i}" } - [super].concat(order_columns).join(', ') + [super, *order_columns].join(', ') end end end diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 72e9272cd7..ff825e52c1 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -245,9 +245,9 @@ module ActiveRecord def construct_limited_ids_condition(relation) orders = relation.order_values.map { |val| val.presence }.compact - values = @klass.connection.distinct("#{quoted_table_name}.#{primary_key}", orders) + values = @klass.connection.columns_for_distinct("#{quoted_table_name}.#{quoted_primary_key}", orders) - relation = relation.dup.select(values) + relation = relation.dup.select(values).distinct! id_rows = @klass.connection.select_all(relation.arel, 'SQL', relation.bind_values) ids_array = id_rows.map {|row| row[primary_key]} -- cgit v1.2.3 From 1cc63e94dbe52a9cf01a71c1dc45ee18443f97dc Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Fri, 10 May 2013 16:21:59 +0200 Subject: Don't try to EXPLAIN select_db calls --- activerecord/lib/active_record/explain_subscriber.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/explain_subscriber.rb b/activerecord/lib/active_record/explain_subscriber.rb index a3bc56d600..6a49936644 100644 --- a/activerecord/lib/active_record/explain_subscriber.rb +++ b/activerecord/lib/active_record/explain_subscriber.rb @@ -19,7 +19,7 @@ module ActiveRecord # On the other hand, we want to monitor the performance of our real database # queries, not the performance of the access to the query cache. IGNORED_PAYLOADS = %w(SCHEMA EXPLAIN CACHE) - EXPLAINED_SQLS = /\A\s*(select|update|delete|insert)/i + EXPLAINED_SQLS = /\A\s*(select|update|delete|insert)\b/i def ignore_payload?(payload) payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name]) || payload[:sql] !~ EXPLAINED_SQLS end -- cgit v1.2.3 From 32a5cad1e46027cf8234cdb8d556f307b86d4c17 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 12 Apr 2013 12:12:37 +0100 Subject: Move #proxy_association method to AssociationRelation --- activerecord/lib/active_record/association_relation.rb | 4 ++++ activerecord/lib/active_record/associations/collection_proxy.rb | 4 +--- activerecord/lib/active_record/relation.rb | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/association_relation.rb b/activerecord/lib/active_record/association_relation.rb index e2976577f6..20516bba0c 100644 --- a/activerecord/lib/active_record/association_relation.rb +++ b/activerecord/lib/active_record/association_relation.rb @@ -5,6 +5,10 @@ module ActiveRecord @association = association end + def proxy_association + @association + end + private def exec_queries diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 56e57cc36e..71b64de5ea 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -847,9 +847,7 @@ module ActiveRecord # Returns a Relation object for the records in this association def scope - @association.scope.tap do |scope| - scope.proxy_association = @association - end + @association.scope end # :nodoc: diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 913f6f88f2..ae3fa85da9 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -17,7 +17,7 @@ module ActiveRecord include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches, Explain, Delegation attr_reader :table, :klass, :loaded - attr_accessor :default_scoped, :proxy_association + attr_accessor :default_scoped alias :model :klass alias :loaded? :loaded alias :default_scoped? :default_scoped -- cgit v1.2.3 From ec75ff34517c98d8feb6ad81ae79c44e611b92e7 Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Fri, 10 May 2013 11:37:58 +0200 Subject: Reject blank order_values within #columns_for_distinct, as the orders aren't used at all on non-postgres adapters. --- .../active_record/connection_adapters/postgresql/schema_statements.rb | 2 +- activerecord/lib/active_record/relation/finder_methods.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index 8feee23df0..a651b6c32e 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -470,7 +470,7 @@ module ActiveRecord # PostgreSQL requires the ORDER BY columns in the select list for distinct queries, and # requires that the ORDER BY include the distinct column. def columns_for_distinct(columns, orders) #:nodoc: - order_columns = orders.map{ |s| + order_columns = orders.reject(&:blank?).map{ |s| # Convert Arel node to string s = s.to_sql unless s.is_a?(String) # Remove any ASC/DESC modifiers diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index ff825e52c1..a51db614cd 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -244,8 +244,8 @@ module ActiveRecord end def construct_limited_ids_condition(relation) - orders = relation.order_values.map { |val| val.presence }.compact - values = @klass.connection.columns_for_distinct("#{quoted_table_name}.#{quoted_primary_key}", orders) + values = @klass.connection.columns_for_distinct( + "#{quoted_table_name}.#{quoted_primary_key}", relation.order_values) relation = relation.dup.select(values).distinct! -- cgit v1.2.3 From cd04a99ba4b5227fb103b6d4e7504c770833e612 Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Fri, 10 May 2013 11:39:08 +0200 Subject: Move the except(:select) inside the construct_limited_ids_condition method to pair it closely with its motivation. --- activerecord/lib/active_record/relation/finder_methods.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index a51db614cd..531343782e 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -234,7 +234,7 @@ module ActiveRecord limitable_reflections = using_limitable_reflections?(join_dependency.reflections) if !limitable_reflections && relation.limit_value - limited_id_condition = construct_limited_ids_condition(relation.except(:select)) + limited_id_condition = construct_limited_ids_condition(relation) relation = relation.where(limited_id_condition) end @@ -247,7 +247,7 @@ module ActiveRecord values = @klass.connection.columns_for_distinct( "#{quoted_table_name}.#{quoted_primary_key}", relation.order_values) - relation = relation.dup.select(values).distinct! + relation = relation.except(:select).select(values).distinct! id_rows = @klass.connection.select_all(relation.arel, 'SQL', relation.bind_values) ids_array = id_rows.map {|row| row[primary_key]} -- cgit v1.2.3 From 1dcb1ccc9d3d4f41e8f1a76ff3465f708189dd2f Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Fri, 10 May 2013 11:42:24 +0200 Subject: Simplify conditions within apply_join_dependency --- activerecord/lib/active_record/relation/finder_methods.rb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 531343782e..9a774b5abc 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -231,16 +231,12 @@ module ActiveRecord relation = association.join_relation(relation) end - limitable_reflections = using_limitable_reflections?(join_dependency.reflections) - - if !limitable_reflections && relation.limit_value - limited_id_condition = construct_limited_ids_condition(relation) - relation = relation.where(limited_id_condition) + if using_limitable_reflections?(join_dependency.reflections) + relation + else + relation = relation.where(construct_limited_ids_condition(relation)) if relation.limit_value + relation.except(:limit, :offset) end - - relation = relation.except(:limit, :offset) unless limitable_reflections - - relation end def construct_limited_ids_condition(relation) -- cgit v1.2.3 From 88219cc88aadf75fe57a1ecacbe92a7acef64145 Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Fri, 10 May 2013 11:47:45 +0200 Subject: Pull the excepts into apply_join_dependency, for the sake of DRY. --- activerecord/lib/active_record/relation/finder_methods.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 9a774b5abc..ed5fe2c683 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -217,16 +217,17 @@ module ActiveRecord def construct_relation_for_association_calculations including = (eager_load_values + includes_values).uniq join_dependency = ActiveRecord::Associations::JoinDependency.new(@klass, including, arel.froms.first) - relation = except(:includes, :eager_load, :preload) - apply_join_dependency(relation, join_dependency) + apply_join_dependency(self, join_dependency) end def construct_relation_for_association_find(join_dependency) - relation = except(:includes, :eager_load, :preload, :select).select(join_dependency.columns) + relation = except(:select).select(join_dependency.columns) apply_join_dependency(relation, join_dependency) end def apply_join_dependency(relation, join_dependency) + relation = relation.except(:includes, :eager_load, :preload) + join_dependency.join_associations.each do |association| relation = association.join_relation(relation) end -- cgit v1.2.3 From 75fe7434a81d0aaef8ec2ada8f88b5b20721c25a Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Fri, 10 May 2013 11:53:23 +0200 Subject: DRY-up join dependency creation by extracting construct_join_depdency --- activerecord/lib/active_record/relation/finder_methods.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index ed5fe2c683..d2cc2c7d88 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -160,7 +160,7 @@ module ActiveRecord conditions = conditions.id if Base === conditions return false if !conditions - join_dependency = construct_join_dependency_for_association_find + join_dependency = construct_join_dependency relation = construct_relation_for_association_find(join_dependency) relation = relation.except(:select, :order).select("1 AS one").limit(1) @@ -201,7 +201,7 @@ module ActiveRecord protected def find_with_associations - join_dependency = construct_join_dependency_for_association_find + join_dependency = construct_join_dependency relation = construct_relation_for_association_find(join_dependency) rows = connection.select_all(relation, 'SQL', relation.bind_values.dup) join_dependency.instantiate(rows) @@ -209,15 +209,13 @@ module ActiveRecord [] end - def construct_join_dependency_for_association_find + def construct_join_dependency(joins = []) including = (eager_load_values + includes_values).uniq - ActiveRecord::Associations::JoinDependency.new(@klass, including, []) + ActiveRecord::Associations::JoinDependency.new(@klass, including, joins) end def construct_relation_for_association_calculations - including = (eager_load_values + includes_values).uniq - join_dependency = ActiveRecord::Associations::JoinDependency.new(@klass, including, arel.froms.first) - apply_join_dependency(self, join_dependency) + apply_join_dependency(self, construct_join_dependency(arel.froms.first)) end def construct_relation_for_association_find(join_dependency) -- cgit v1.2.3 From 35c198ca9bc1ec530fc29b686978511f23cee076 Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Fri, 10 May 2013 17:34:51 +0200 Subject: In #apply_join_dependency, we can apply the #where in-place because relation is always a new object. Thanks to the #except we call at the top of the method. --- activerecord/lib/active_record/relation/finder_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index d2cc2c7d88..332548ea3e 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -233,7 +233,7 @@ module ActiveRecord if using_limitable_reflections?(join_dependency.reflections) relation else - relation = relation.where(construct_limited_ids_condition(relation)) if relation.limit_value + relation.where!(construct_limited_ids_condition(relation)) if relation.limit_value relation.except(:limit, :offset) end end -- cgit v1.2.3 From fba18f19948f084023fd8744025f57da00163265 Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Fri, 10 May 2013 17:41:29 +0200 Subject: Extract JoinDependency#join_relation to DRY the repeated application of the #join_associations. --- activerecord/lib/active_record/associations/join_dependency.rb | 7 +++++++ activerecord/lib/active_record/relation/finder_methods.rb | 5 +---- activerecord/lib/active_record/relation/merger.rb | 4 +--- 3 files changed, 9 insertions(+), 7 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb index 28e081c03c..5b2f2d1902 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -55,6 +55,13 @@ module ActiveRecord join_parts.first end + def join_relation(relation) + join_associations.each do |association| + relation = association.join_relation(relation) + end + relation + end + def columns join_parts.collect { |join_part| table = join_part.aliased_table diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 332548ea3e..ba222aac93 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -225,10 +225,7 @@ module ActiveRecord def apply_join_dependency(relation, join_dependency) relation = relation.except(:includes, :eager_load, :preload) - - join_dependency.join_associations.each do |association| - relation = association.join_relation(relation) - end + relation = join_dependency.join_relation(relation) if using_limitable_reflections?(join_dependency.reflections) relation diff --git a/activerecord/lib/active_record/relation/merger.rb b/activerecord/lib/active_record/relation/merger.rb index bda7a76330..eea43aff0e 100644 --- a/activerecord/lib/active_record/relation/merger.rb +++ b/activerecord/lib/active_record/relation/merger.rb @@ -94,9 +94,7 @@ module ActiveRecord []) relation.joins! rest - join_dependency.join_associations.each do |association| - @relation = association.join_relation(relation) - end + @relation = join_dependency.join_relation(relation) end end -- cgit v1.2.3 From 3e0c06d8ab1879539964c7b6c805eff870a80cbb Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Sat, 11 May 2013 03:08:08 -0400 Subject: read_attribute_before_type_cast should accept symbol --- activerecord/lib/active_record/attribute_methods/before_type_cast.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/attribute_methods/before_type_cast.rb b/activerecord/lib/active_record/attribute_methods/before_type_cast.rb index a23baeaced..f596a8b02e 100644 --- a/activerecord/lib/active_record/attribute_methods/before_type_cast.rb +++ b/activerecord/lib/active_record/attribute_methods/before_type_cast.rb @@ -41,8 +41,9 @@ module ActiveRecord # task.read_attribute_before_type_cast('id') # => '1' # task.read_attribute('completed_on') # => Sun, 21 Oct 2012 # task.read_attribute_before_type_cast('completed_on') # => "2012-10-21" + # task.read_attribute_before_type_cast(:completed_on) # => "2012-10-21" def read_attribute_before_type_cast(attr_name) - @attributes[attr_name] + @attributes[attr_name.to_s] end # Returns a hash of attributes before typecasting and deserialization. -- cgit v1.2.3 From 0215faf472aa22a0e57147cc529fe91ee3117926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sat, 11 May 2013 12:47:31 -0700 Subject: Merge pull request #10572 from nertzy/dont-modify-options-hash-in-primary-key Don't modify args in TableDefinition#primary_key --- .../active_record/connection_adapters/abstract/schema_definitions.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 566550cbe2..aabedf15e9 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -65,8 +65,7 @@ module ActiveRecord # Appends a primary key definition to the table definition. # Can be called multiple times, but this is probably not a good idea. def primary_key(name, type = :primary_key, options = {}) - options[:primary_key] = true - column(name, type, options) + column(name, type, options.merge(:primary_key => true)) end # Returns a ColumnDefinition for the column with name +name+. -- cgit v1.2.3 From 443f8dd5cdb273327a8b03a97c431de67c15ace6 Mon Sep 17 00:00:00 2001 From: Kyle Stevens Date: Sat, 11 May 2013 22:32:33 -0400 Subject: Call assume_migrated_upto_version on connection Call assume_migrated_upto_version on connection to prevent it from first being picked up in method_missing. In the base class, Migration, method_missing expects the argument to be a table name, and calls proper_table_name on the arguments before sending to connection. If table_name_prefix or table_name_suffix is used, the schema version changes to prefix_version_suffix, breaking `rake test:prepare`. Fixes #10411. --- activerecord/lib/active_record/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/schema.rb b/activerecord/lib/active_record/schema.rb index 3259dbbd80..4bfd0167a4 100644 --- a/activerecord/lib/active_record/schema.rb +++ b/activerecord/lib/active_record/schema.rb @@ -43,7 +43,7 @@ module ActiveRecord unless info[:version].blank? initialize_schema_migrations_table - assume_migrated_upto_version(info[:version], migrations_paths) + connection.assume_migrated_upto_version(info[:version], migrations_paths) end end -- cgit v1.2.3 From 346cda4f348993b45a894c1351e2cca03d72dee2 Mon Sep 17 00:00:00 2001 From: wangjohn Date: Sat, 11 May 2013 22:56:05 -0400 Subject: Adding documentation to the automatic inverse_of finder. --- activerecord/lib/active_record/associations.rb | 21 +++++++++++++++++++++ activerecord/lib/active_record/reflection.rb | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 5e5995f566..b71200cd2a 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -568,6 +568,8 @@ module ActiveRecord # @group.avatars << Avatar.new # this would work if User belonged_to Avatar rather than the other way around # @group.avatars.delete(@group.avatars.last) # so would this # + # == Setting Inverses + # # If you are using a +belongs_to+ on the join model, it is a good idea to set the # :inverse_of option on the +belongs_to+, which will mean that the following example # works correctly (where tags is a +has_many+ :through association): @@ -584,6 +586,25 @@ module ActiveRecord # belongs_to :tag, inverse_of: :taggings # end # + # If you do not set the +:inverse_of+ record, the association will do its + # best to match itself up with the correct inverse. Automatic +:inverse_of+ + # detection only works on :has_many, :has_one, and :belongs_to associations. + # + # Extra options on the associations, as defined in the + # +AssociationReflection::INVALID_AUTOMATIC_INVERSE_OPTIONS+ constant, will + # also prevent the association's inverse from being found automatically. + # + # The automatic guessing of the inverse association uses a heuristic based + # on the name of the class, so it may not work for all associations, + # especially the ones with non-standard names. + # + # You can turn off the automatic detection of inverse associations by setting + # the +:automatic_inverse_of+ option to +false+ like so: + # + # class Taggable < ActiveRecord::Base + # belongs_to :tag, automatic_inverse_of: false + # end + # # == Nested Associations # # You can actually specify *any* association with the :through option, including an diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 0ba860a186..d922e23e39 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -450,7 +450,7 @@ module ActiveRecord # us from being able to guess the inverse automatically. First, the # +automatic_inverse_of+ option cannot be set to false. Second, we must # have :has_many, :has_one, :belongs_to associations. Third, we must - # not have options such as :class_name or :polymorphic which prevent us + # not have options such as :polymorphic or :foreign_key which prevent us # from correctly guessing the inverse association. # # Anything with a scope can additionally ruin our attempt at finding an -- cgit v1.2.3 From 1a9766f1280f9550ba1de4538fccd5ad51c557ac Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Sun, 12 May 2013 15:52:09 +0530 Subject: copy edits [ci skip] --- activerecord/lib/active_record/migration.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 4a04748680..511a1585a7 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -668,11 +668,7 @@ module ActiveRecord copied end - # Determines the version number of the next migration - # if the timestamped migrations are activated then the comparison with the current time is made - # and then higer of the two values is selected - # For non timestamped values, the simple numbers are used in the format of "057", "570" - + # Determines the version number of the next migration. def next_migration_number(number) if ActiveRecord::Base.timestamped_migrations [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % number].max -- cgit v1.2.3 From 33062ee0f1695f548a536177bfbb0e0990fd8228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sun, 12 May 2013 01:03:43 -0300 Subject: Some editorial changes on the documentation. * Remove some autolinks * Fix the rendered result * Change sql to SQL [ci skip] --- activerecord/lib/active_record/associations.rb | 46 +++++++++++++------------- activerecord/lib/active_record/reflection.rb | 8 ++--- 2 files changed, 27 insertions(+), 27 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index b71200cd2a..3490057298 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -172,7 +172,7 @@ module ActiveRecord @association_cache[name] = association end - # Associations are a set of macro-like class methods for tying objects together through + # \Associations are a set of macro-like class methods for tying objects together through # foreign keys. They express relationships like "Project has one Project Manager" # or "Project belongs to a Portfolio". Each macro adds a number of methods to the # class which are specialized according to the collection or association symbol and the @@ -365,11 +365,11 @@ module ActiveRecord # there is some special behavior you should be aware of, mostly involving the saving of # associated objects. # - # You can set the :autosave option on a has_one, belongs_to, + # You can set the :autosave option on a has_one, belongs_to, # has_many, or has_and_belongs_to_many association. Setting it # to +true+ will _always_ save the members, whereas setting it to +false+ will - # _never_ save the members. More details about :autosave option is available at - # autosave_association.rb . + # _never_ save the members. More details about :autosave option is available at + # AutosaveAssociation. # # === One-to-one associations # @@ -402,7 +402,7 @@ module ActiveRecord # # == Customizing the query # - # Associations are built from Relations, and you can use the Relation syntax + # \Associations are built from Relations, and you can use the Relation syntax # to customize them. For example, to add a condition: # # class Blog < ActiveRecord::Base @@ -588,10 +588,10 @@ module ActiveRecord # # If you do not set the +:inverse_of+ record, the association will do its # best to match itself up with the correct inverse. Automatic +:inverse_of+ - # detection only works on :has_many, :has_one, and :belongs_to associations. + # detection only works on +has_many+, +has_one+, and +belongs_to+ associations. # # Extra options on the associations, as defined in the - # +AssociationReflection::INVALID_AUTOMATIC_INVERSE_OPTIONS+ constant, will + # AssociationReflection::INVALID_AUTOMATIC_INVERSE_OPTIONS constant, will # also prevent the association's inverse from being found automatically. # # The automatic guessing of the inverse association uses a heuristic based @@ -605,7 +605,7 @@ module ActiveRecord # belongs_to :tag, automatic_inverse_of: false # end # - # == Nested Associations + # == Nested \Associations # # You can actually specify *any* association with the :through option, including an # association which has a :through option itself. For example: @@ -648,7 +648,7 @@ module ActiveRecord # add a Commenter in the example above, there would be no way to tell how to set up the # intermediate Post and Comment objects. # - # == Polymorphic Associations + # == Polymorphic \Associations # # Polymorphic associations on models are not restricted on what types of models they # can be associated with. Rather, they specify an interface that a +has_many+ association @@ -810,7 +810,7 @@ module ActiveRecord # For example if all the addressables are either of class Person or Company then a total # of 3 queries will be executed. The list of addressable types to load is determined on # the back of the addresses loaded. This is not supported if Active Record has to fallback - # to the previous implementation of eager loading and will raise ActiveRecord::EagerLoadPolymorphicError. + # to the previous implementation of eager loading and will raise ActiveRecord::EagerLoadPolymorphicError. # The reason is that the parent model's type is a column value so its corresponding table # name cannot be put in the +FROM+/+JOIN+ clauses of that query. # @@ -1045,7 +1045,7 @@ module ActiveRecord # An empty array is returned if none are found. # [collection<<(object, ...)] # Adds one or more objects to the collection by setting their foreign keys to the collection's primary key. - # Note that this operation instantly fires update sql without waiting for the save or update call on the + # Note that this operation instantly fires update SQL without waiting for the save or update call on the # parent object, unless the parent object is a new record. # [collection.delete(object, ...)] # Removes one or more objects from the collection by setting their foreign keys to +NULL+. @@ -1081,10 +1081,10 @@ module ActiveRecord # [collection.size] # Returns the number of associated objects. # [collection.find(...)] - # Finds an associated object according to the same rules as ActiveRecord::Base.find. + # Finds an associated object according to the same rules as ActiveRecord::Base.find. # [collection.exists?(...)] # Checks whether an associated object with the given conditions exists. - # Uses the same rules as ActiveRecord::Base.exists?. + # Uses the same rules as ActiveRecord::Base.exists?. # [collection.build(attributes = {}, ...)] # Returns one or more new objects of the collection type that have been instantiated # with +attributes+ and linked to this object through a foreign key, but have not yet @@ -1103,7 +1103,7 @@ module ActiveRecord # # === Example # - # Example: A Firm class declares has_many :clients, which will add: + # A Firm class declares has_many :clients, which will add: # * Firm#clients (similar to Client.where(firm_id: id)) # * Firm#clients<< # * Firm#clients.delete @@ -1137,8 +1137,8 @@ module ActiveRecord # Controls what happens to the associated objects when # their owner is destroyed. Note that these are implemented as # callbacks, and Rails executes callbacks in order. Therefore, other - # similar callbacks may affect the :dependent behavior, and the - # :dependent behavior may affect other callbacks. + # similar callbacks may affect the :dependent behavior, and the + # :dependent behavior may affect other callbacks. # # * :destroy causes all the associated objects to also be destroyed. # * :delete_all causes all the associated objects to be deleted directly from the database (so callbacks will not be executed). @@ -1184,8 +1184,8 @@ module ActiveRecord # If true, always save the associated objects or destroy them if marked for destruction, # when saving the parent object. If false, never save or destroy the associated objects. # By default, only save associated objects that are new records. This option is implemented as a - # before_save callback. Because callbacks are run in the order they are defined, associated objects - # may need to be explicitly saved in any user-defined before_save callbacks. + # +before_save+ callback. Because callbacks are run in the order they are defined, associated objects + # may need to be explicitly saved in any user-defined +before_save+ callbacks. # # Note that accepts_nested_attributes_for sets :autosave to true. # [:inverse_of] @@ -1210,7 +1210,7 @@ module ActiveRecord # Specifies a one-to-one association with another class. This method should only be used # if the other class contains the foreign key. If the current class contains the foreign key, # then you should use +belongs_to+ instead. See also ActiveRecord::Associations::ClassMethods's overview - # on when to use has_one and when to use belongs_to. + # on when to use +has_one+ and when to use +belongs_to+. # # The following methods for retrieval and query of a single associated object will be added: # @@ -1378,7 +1378,7 @@ module ActiveRecord # class is created and decremented when it's destroyed. This requires that a column # named #{table_name}_count (such as +comments_count+ for a belonging Comment class) # is used on the associate class (such as a Post class) - that is the migration for - # #{table_name}_count is created on the associate class (such that Post.comments_count will + # #{table_name}_count is created on the associate class (such that Post.comments_count will # return the count cached, see note below). You can also specify a custom counter # cache column by providing a column name instead of a +true+/+false+ value to this # option (e.g., counter_cache: :my_custom_counter.) @@ -1460,7 +1460,7 @@ module ActiveRecord # [collection<<(object, ...)] # Adds one or more objects to the collection by creating associations in the join table # (collection.push and collection.concat are aliases to this method). - # Note that this operation instantly fires update sql without waiting for the save or update call on the + # Note that this operation instantly fires update SQL without waiting for the save or update call on the # parent object, unless the parent object is a new record. # [collection.delete(object, ...)] # Removes one or more objects from the collection by removing their associations from the join table. @@ -1483,10 +1483,10 @@ module ActiveRecord # [collection.find(id)] # Finds an associated object responding to the +id+ and that # meets the condition that it has to be associated with this object. - # Uses the same rules as ActiveRecord::Base.find. + # Uses the same rules as ActiveRecord::Base.find. # [collection.exists?(...)] # Checks whether an associated object with the given conditions exists. - # Uses the same rules as ActiveRecord::Base.exists?. + # Uses the same rules as ActiveRecord::Base.exists?. # [collection.build(attributes = {})] # Returns a new object of the collection type that has been instantiated # with +attributes+ and linked to this object through the join table, but has not yet been saved. diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index dc082b96f4..1f76adb367 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -8,7 +8,7 @@ module ActiveRecord self.reflections = {} end - # Reflection enables to interrogate Active Record classes and objects + # \Reflection enables to interrogate Active Record classes and objects # about their associations and aggregations. This information can, # for example, be used in a form builder that takes an Active Record object # and creates input fields for all of the attributes depending on their type @@ -100,7 +100,7 @@ module ActiveRecord # Returns the hash of options used for the macro. # # composed_of :balance, class_name: 'Money' returns { class_name: "Money" } - # has_many :clients returns +{}+ + # has_many :clients returns {} attr_reader :options attr_reader :active_record @@ -449,8 +449,8 @@ module ActiveRecord # Checks to see if the reflection doesn't have any options that prevent # us from being able to guess the inverse automatically. First, the # +automatic_inverse_of+ option cannot be set to false. Second, we must - # have :has_many, :has_one, :belongs_to associations. Third, we must - # not have options such as :polymorphic or :foreign_key which prevent us + # have +has_many+, +has_one+, +belongs_to+ associations. Third, we must + # not have options such as +:polymorphic+ or +:foreign_key+ which prevent us # from correctly guessing the inverse association. # # Anything with a scope can additionally ruin our attempt at finding an -- cgit v1.2.3 From 8d3c67fbc44844d5565b580655ab9705d9fb8a08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sun, 12 May 2013 10:49:49 -0700 Subject: Merge pull request #10556 from Empact/deprecate-schema-statements-distinct Deprecate SchemaStatements#distinct, and make SchemaStatements#columns_for_distinct nodoc. Conflicts: activerecord/CHANGELOG.md --- .../active_record/connection_adapters/abstract/schema_statements.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') 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 6e1f43cce6..8ffe150de6 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -710,6 +710,7 @@ module ActiveRecord # distinct("posts.id", ["posts.created_at desc"]) # def distinct(columns, order_by) + ActiveSupport::Deprecation.warn("#distinct is deprecated and shall be removed from future releases.") "DISTINCT #{columns_for_distinct(columns, order_by)}" end @@ -718,7 +719,7 @@ module ActiveRecord # require the order columns appear in the SELECT. # # columns_for_distinct("posts.id", ["posts.created_at desc"]) - def columns_for_distinct(columns, orders) + def columns_for_distinct(columns, orders) # :nodoc: columns end -- cgit v1.2.3 From 9b66187622453679497ad2fed4f333e2fca32150 Mon Sep 17 00:00:00 2001 From: Alexey Noskov Date: Tue, 14 May 2013 17:44:32 +0400 Subject: Support array as root element in JSON --- activerecord/lib/active_record/connection_adapters/postgresql/cast.rb | 2 +- .../lib/active_record/connection_adapters/postgresql/quoting.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb index a9ef11aa83..a73f0ac57f 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb @@ -60,7 +60,7 @@ module ActiveRecord end def json_to_string(object) - if Hash === object + if Hash === object || Array === object ActiveSupport::JSON.encode(object) else object diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb index 40a3b82839..e9daa5d7ff 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb @@ -30,6 +30,7 @@ module ActiveRecord when Array case sql_type when 'point' then super(PostgreSQLColumn.point_to_string(value)) + when 'json' then super(PostgreSQLColumn.json_to_string(value)) else if column.array "'#{PostgreSQLColumn.array_to_string(value, column, self).gsub(/'/, "''")}'" @@ -98,6 +99,7 @@ module ActiveRecord when Array case column.sql_type when 'point' then PostgreSQLColumn.point_to_string(value) + when 'json' then PostgreSQLColumn.json_to_string(value) else return super(value, column) unless column.array PostgreSQLColumn.array_to_string(value, column, self) -- cgit v1.2.3 From ba552764344bc0a3c25b8576ec11f127ceaa16da Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 14 May 2013 16:03:09 -0700 Subject: deprecating string based terminators --- activerecord/lib/active_record/transactions.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index a5955ccba4..77634b40bb 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -10,7 +10,9 @@ module ActiveRecord end included do - define_callbacks :commit, :rollback, :terminator => "result == false", :scope => [:kind, :name] + define_callbacks :commit, :rollback, + terminator: ->(_, result) { result == false }, + scope: [:kind, :name] end # = Active Record Transactions -- cgit v1.2.3 From 349fc90996b6372caf5fcfdad2c26b319868b9c4 Mon Sep 17 00:00:00 2001 From: kennyj Date: Wed, 8 May 2013 02:45:35 +0900 Subject: Also support extensions in PostgreSQL 9.1, because this has been supported since 9.1. --- .../lib/active_record/connection_adapters/postgresql_adapter.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 88b09e7999..d5a603cadc 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -622,9 +622,9 @@ module ActiveRecord true end - # Returns true if pg > 9.2 + # Returns true if pg > 9.1 def supports_extensions? - postgresql_version >= 90200 + postgresql_version >= 90100 end # Range datatypes weren't introduced until PostgreSQL 9.2 @@ -646,9 +646,9 @@ module ActiveRecord def extension_enabled?(name) if supports_extensions? - res = exec_query "SELECT EXISTS(SELECT * FROM pg_available_extensions WHERE name = '#{name}' AND installed_version IS NOT NULL)", + res = exec_query "SELECT EXISTS(SELECT * FROM pg_available_extensions WHERE name = '#{name}' AND installed_version IS NOT NULL) as enabled", 'SCHEMA' - res.column_types['exists'].type_cast res.rows.first.first + res.column_types['enabled'].type_cast res.rows.first.first end end -- cgit v1.2.3 From dcc1267feeff2bed4174c618ef3050615d0bafb0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 15 May 2013 10:11:16 -0700 Subject: require things we need --- activerecord/lib/active_record/attribute_assignment.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/attribute_assignment.rb b/activerecord/lib/active_record/attribute_assignment.rb index e536f5ebcc..a13bb9299a 100644 --- a/activerecord/lib/active_record/attribute_assignment.rb +++ b/activerecord/lib/active_record/attribute_assignment.rb @@ -1,3 +1,4 @@ +require 'active_model/forbidden_attributes_protection' module ActiveRecord module AttributeAssignment -- cgit v1.2.3 From 99860582b2b1c0fc42bf84c52aac57b243d42678 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 15 May 2013 18:03:10 -0700 Subject: tiny types should only be integers when the length is <= 1. fixes #10620 --- .../lib/active_record/connection_adapters/mysql_adapter.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index f23521430d..1826d88500 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -393,6 +393,14 @@ module ActiveRecord TYPES[new] = TYPES[old] end + def self.find_type(field) + if field.type == Mysql::Field::TYPE_TINY && field.length > 1 + TYPES[Mysql::Field::TYPE_LONG] + else + TYPES.fetch(field.type) { Fields::Identity.new } + end + end + register_type Mysql::Field::TYPE_TINY, Fields::Boolean.new register_type Mysql::Field::TYPE_LONG, Fields::Integer.new alias_type Mysql::Field::TYPE_LONGLONG, Mysql::Field::TYPE_LONG @@ -425,9 +433,7 @@ module ActiveRecord if field.decimals > 0 types[field.name] = Fields::Decimal.new else - types[field.name] = Fields::TYPES.fetch(field.type) { - Fields::Identity.new - } + types[field.name] = Fields.find_type field end } result_set = ActiveRecord::Result.new(types.keys, result.to_a, types) -- cgit v1.2.3 From 2583e8ade8280d61f31ce4e20b1c0908dffb5b9b Mon Sep 17 00:00:00 2001 From: Joel Cogen Date: Thu, 16 May 2013 12:06:08 +0200 Subject: Fix detection of engine in rake db:load_config Broken by d1d7c86d0c8dcb7e75a87644b330c4e9e7d6c1c1 --- activerecord/lib/active_record/railtie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index e36888d4a8..709ad5a2ad 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -44,7 +44,7 @@ module ActiveRecord ActiveRecord::Tasks::DatabaseTasks.migrations_paths = Rails.application.paths['db/migrate'].to_a ActiveRecord::Tasks::DatabaseTasks.fixtures_path = File.join Rails.root, 'test', 'fixtures' - if defined?(ENGINE_PATH) && engine = Rails::Engine.find(ENGINE_PATH) + if engine = Rails::Engine.find(find_engine_path(APP_RAKEFILE)) if engine.paths['db/migrate'].existent ActiveRecord::Tasks::DatabaseTasks.migrations_paths += engine.paths['db/migrate'].to_a end -- cgit v1.2.3 From 89f558c8c6a1f04883456fca573c27b8f72d09ab Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Thu, 16 May 2013 15:52:12 +0200 Subject: Check if APP_RAKEFILE is defined --- activerecord/lib/active_record/railtie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 709ad5a2ad..31a0ace864 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -44,7 +44,7 @@ module ActiveRecord ActiveRecord::Tasks::DatabaseTasks.migrations_paths = Rails.application.paths['db/migrate'].to_a ActiveRecord::Tasks::DatabaseTasks.fixtures_path = File.join Rails.root, 'test', 'fixtures' - if engine = Rails::Engine.find(find_engine_path(APP_RAKEFILE)) + if defined?(APP_RAKEFILE) && engine = Rails::Engine.find(find_engine_path(APP_RAKEFILE)) if engine.paths['db/migrate'].existent ActiveRecord::Tasks::DatabaseTasks.migrations_paths += engine.paths['db/migrate'].to_a end -- cgit v1.2.3 From 711097e6a5af61a31a0547223038a4b5e1d59366 Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Mon, 13 May 2013 10:54:59 +0300 Subject: Add more data to AR::UnknownAttributeError begin Topic.new("hello" => "world") rescue ActiveRecord::UnknownAttributeError => e e.record # => # e.attribute # => "hello" end --- activerecord/lib/active_record/attribute_assignment.rb | 2 +- activerecord/lib/active_record/errors.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/attribute_assignment.rb b/activerecord/lib/active_record/attribute_assignment.rb index a13bb9299a..75377bba57 100644 --- a/activerecord/lib/active_record/attribute_assignment.rb +++ b/activerecord/lib/active_record/attribute_assignment.rb @@ -45,7 +45,7 @@ module ActiveRecord if respond_to?("#{k}=") raise else - raise UnknownAttributeError, "unknown attribute: #{k}" + raise UnknownAttributeError.new(self, k) end end diff --git a/activerecord/lib/active_record/errors.rb b/activerecord/lib/active_record/errors.rb index cd31147414..017b8bace6 100644 --- a/activerecord/lib/active_record/errors.rb +++ b/activerecord/lib/active_record/errors.rb @@ -159,6 +159,15 @@ module ActiveRecord # Raised when unknown attributes are supplied via mass assignment. class UnknownAttributeError < NoMethodError + + attr_reader :record, :attribute + + def initialize(record, attribute) + @record = record + @attribute = attribute.to_s + super("unknown attribute: #{attribute}") + end + end # Raised when an error occurred while doing a mass assignment to an attribute through the -- cgit v1.2.3 From 6062e42f982e0641de023419dc357bf1ec6b5e35 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 16 May 2013 16:06:26 -0700 Subject: let Ruby do the is_a check for us --- activerecord/lib/active_record/inheritance.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb index 8df76c7f5f..40976bc29e 100644 --- a/activerecord/lib/active_record/inheritance.rb +++ b/activerecord/lib/active_record/inheritance.rb @@ -116,9 +116,10 @@ module ActiveRecord begin constant = ActiveSupport::Dependencies.constantize(candidate) return constant if candidate == constant.to_s - rescue NameError => e - # We don't want to swallow NoMethodError < NameError errors - raise e unless e.instance_of?(NameError) + # We don't want to swallow NoMethodError < NameError errors + rescue NoMethodError + raise + rescue NameError end end -- cgit v1.2.3