Skip to content

Commit

Permalink
Merge pull request #14572 from laurocaetano/with_options_and_scope
Browse files Browse the repository at this point in the history
Fix error when using `with_options` with lambda.

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information
rafaelfranca committed Apr 3, 2014
2 parents 6da3e9a + db5d26c commit ae110ce
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
* Fixed error when using `with_options` with lambda.

Fixes #9805.

*Lauro Caetano*

* Switch `sqlite3:///` URLs (which were temporarily
deprecated in 4.1) from relative to absolute.

Expand Down
Expand Up @@ -24,6 +24,8 @@
require 'models/speedometer'
require 'models/reference'
require 'models/job'
require 'models/college'
require 'models/student'

class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase
fixtures :authors, :posts, :comments
Expand Down Expand Up @@ -65,6 +67,13 @@ def test_anonymous_has_many
dev.developer_projects.map(&:project_id).sort
end

def test_has_many_build_with_options
college = College.create(name: 'UFMT')
student = Student.create(active: true, college_id: college.id, name: 'Sarah')

assert_equal college.students, Student.where(active: true, college_id: college.id)
end

def test_create_from_association_should_respect_default_scope
car = Car.create(:name => 'honda')
assert_equal 'honda', car.name
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/models/college.rb
@@ -1,5 +1,10 @@
require_dependency 'models/arunit2_model'
require 'active_support/core_ext/object/with_options'

class College < ARUnit2Model
has_many :courses

with_options dependent: :destroy do |assoc|
assoc.has_many :students, -> { where(active: true) }
end
end
1 change: 1 addition & 0 deletions activerecord/test/models/student.rb
@@ -1,3 +1,4 @@
class Student < ActiveRecord::Base
has_and_belongs_to_many :lessons
belongs_to :college
end
2 changes: 2 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -638,6 +638,8 @@ def create_table(*args, &block)

create_table :students, force: true do |t|
t.string :name
t.boolean :active
t.integer :college_id
end

create_table :subscribers, force: true, id: false do |t|
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/option_merger.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(context, options)

private
def method_missing(method, *arguments, &block)
if arguments.last.is_a?(Proc)
if arguments.first.is_a?(Proc)
proc = arguments.pop
arguments << lambda { |*args| @options.deep_merge(proc.call(*args)) }
else
Expand Down

0 comments on commit ae110ce

Please sign in to comment.