aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md63
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_adapter.rb11
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb2
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb2
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb2
-rw-r--r--activerecord/lib/active_record/scoping/named.rb15
-rw-r--r--activerecord/test/cases/named_scope_test.rb10
-rw-r--r--activerecord/test/cases/relations_test.rb7
8 files changed, 100 insertions, 12 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 0f339d39e8..2db3c0cdee 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,6 +1,7 @@
## Rails 4.0.0 (unreleased) ##
-* When `:name` option is provided to `remove_index`, use it if there is no index by the conventional name.
+* When `:name` option is provided to `remove_index`, use it if there is no
+ index by the conventional name.
For example, previously if an index was removed like so
`remove_index :values, column: :value, name: 'a_different_name'`
@@ -10,6 +11,66 @@
*Ezekiel Smithburg*
+* Created block to by-pass the prepared statement bindings.
+ This will allow to compose fragments of large SQL statements to
+ avoid multiple round-trips between Ruby and the DB.
+
+ Example:
+
+ sql = Post.connection.unprepared_statement do
+ Post.first.comments.to_sql
+ end
+
+ *Cédric Fabianski*
+
+* Change the semantics of combining scopes to be the same as combining
+ class methods which return scopes. For example:
+
+ class User < ActiveRecord::Base
+ scope :active, -> { where state: 'active' }
+ scope :inactive, -> { where state: 'inactive' }
+ end
+
+ class Post < ActiveRecord::Base
+ def self.active
+ where state: 'active'
+ end
+
+ def self.inactive
+ where state: 'inactive'
+ end
+ end
+
+ ### BEFORE ###
+
+ User.where(state: 'active').where(state: 'inactive')
+ # => SELECT * FROM users WHERE state = 'active' AND state = 'inactive'
+
+ User.active.inactive
+ # => SELECT * FROM users WHERE state = 'inactive'
+
+ Post.active.inactive
+ # => SELECT * FROM posts WHERE state = 'active' AND state = 'inactive'
+
+ ### AFTER ###
+
+ User.active.inactive
+ # => SELECT * FROM posts WHERE state = 'active' AND state = 'inactive'
+
+ Before this change, invoking a scope would merge it into the current
+ scope and return the result. `Relation#merge` applies "last where
+ wins" logic to de-duplicate the conditions, but this lead to
+ confusing and inconsistent behaviour. This fixes that.
+
+ If you really do want the "last where wins" logic, you can opt-in to
+ it like so:
+
+ User.active.merge(User.inactive)
+
+ Fixes #7365.
+
+ *Neeraj Singh* and *Jon Leighton*
+
* Expand `#cache_key` to consult all relevant updated timestamps.
Previously only `updated_at` column was checked, now it will
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
index ff9de712bc..7949bcb5ce 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -118,6 +118,17 @@ module ActiveRecord
@in_use = false
end
+ def unprepared_visitor
+ self.class::BindSubstitution.new self
+ end
+
+ def unprepared_statement
+ old, @visitor = @visitor, unprepared_visitor
+ yield
+ ensure
+ @visitor = old
+ end
+
# Returns the human-readable name of the adapter. Use mixed case - one
# can always use downcase if needed.
def adapter_name
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 be4a30aed9..f88f5742a8 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -143,7 +143,7 @@ module ActiveRecord
if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
@visitor = Arel::Visitors::MySQL.new self
else
- @visitor = BindSubstitution.new self
+ @visitor = unprepared_visitor
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index c91e1b3fb9..691b2ab37f 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -489,7 +489,7 @@ module ActiveRecord
if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
@visitor = Arel::Visitors::PostgreSQL.new self
else
- @visitor = BindSubstitution.new self
+ @visitor = unprepared_visitor
end
@connection_parameters, @config = connection_parameters, config
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
index 981c4c96a0..84fa1c7d5a 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -113,7 +113,7 @@ module ActiveRecord
if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
@visitor = Arel::Visitors::SQLite.new self
else
- @visitor = BindSubstitution.new self
+ @visitor = unprepared_visitor
end
end
diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb
index 01fbb96b8e..12317601b6 100644
--- a/activerecord/lib/active_record/scoping/named.rb
+++ b/activerecord/lib/active_record/scoping/named.rb
@@ -159,10 +159,19 @@ module ActiveRecord
end
singleton_class.send(:define_method, name) do |*args|
- options = body.respond_to?(:call) ? unscoped { body.call(*args) } : body
- relation = all.merge(options)
+ if body.respond_to?(:call)
+ scope = extension ? body.call(*args).extending(extension) : body.call(*args)
- extension ? relation.extending(extension) : relation
+ if scope
+ default_scoped = scope.default_scoped
+ scope = relation.merge(scope)
+ scope.default_scoped = default_scoped
+ end
+ else
+ scope = body
+ end
+
+ scope || all
end
end
end
diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb
index bd121126e7..b593270352 100644
--- a/activerecord/test/cases/named_scope_test.rb
+++ b/activerecord/test/cases/named_scope_test.rb
@@ -309,7 +309,7 @@ class NamedScopeTest < ActiveRecord::TestCase
assert_equal post.comments.size, Post.joins(join).joins(join).where("posts.id = #{post.id}").size
end
- def test_chaining_should_use_latest_conditions_when_creating
+ def test_chaining_applies_last_conditions_when_creating
post = Topic.rejected.new
assert !post.approved?
@@ -323,13 +323,13 @@ class NamedScopeTest < ActiveRecord::TestCase
assert post.approved?
end
- def test_chaining_should_use_latest_conditions_when_searching
+ def test_chaining_combines_conditions_when_searching
# Normal hash conditions
- assert_equal Topic.where(:approved => true).to_a, Topic.rejected.approved.to_a
- assert_equal Topic.where(:approved => false).to_a, Topic.approved.rejected.to_a
+ assert_equal Topic.where(approved: false).where(approved: true).to_a, Topic.rejected.approved.to_a
+ assert_equal Topic.where(approved: true).where(approved: false).to_a, Topic.approved.rejected.to_a
# Nested hash conditions with same keys
- assert_equal [posts(:sti_comments)], Post.with_special_comments.with_very_special_comments.to_a
+ assert_equal [], Post.with_special_comments.with_very_special_comments.to_a
# Nested hash conditions with different keys
assert_equal [posts(:sti_comments)], Post.with_special_comments.with_post(4).to_a.uniq
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 8298d7534c..26cbb03892 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -714,6 +714,13 @@ class RelationTest < ActiveRecord::TestCase
assert_equal [developers(:poor_jamis)], dev_with_count.to_a
end
+ def test_relation_to_sql
+ sql = Post.connection.unprepared_statement do
+ Post.first.comments.to_sql
+ end
+ assert_no_match(/\?/, sql)
+ end
+
def test_relation_merging_with_arel_equalities_keeps_last_equality
devs = Developer.where(Developer.arel_table[:salary].eq(80000)).merge(
Developer.where(Developer.arel_table[:salary].eq(9000))