Permalink
Browse files

Merge pull request #14572 from laurocaetano/with_options_and_scope

Fix error when using `with_options` with lambda.

Conflicts:
	activerecord/CHANGELOG.md

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/test/cases/associations/has_many_associations_test.rb
  • Loading branch information...
1 parent 13d5db7 commit c5af132a2486a3c2888390c54cd37b20c092876f @rafaelfranca rafaelfranca committed Apr 3, 2014
@@ -1,3 +1,9 @@
+* Fixed error when using `with_options` with lambda.
+
+ Fixes #9805.
+
+ *Lauro Caetano*
+
* Fixed error when specifying a non-empty default value on a PostgreSQL array column.
Fixes #10613.
@@ -22,6 +22,8 @@
require 'models/categorization'
require 'models/minivan'
require 'models/speedometer'
+require 'models/college'
+require 'models/student'
class HasManyAssociationsTestForCountWithFinderSql < ActiveRecord::TestCase
class Invoice < ActiveRecord::Base
@@ -118,6 +120,13 @@ def setup
Client.destroyed_client_ids.clear
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
@@ -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,3 +1,4 @@
class Student < ActiveRecord::Base
has_and_belongs_to_many :lessons
+ belongs_to :college
end
@@ -635,6 +635,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|
@@ -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

0 comments on commit c5af132

Please sign in to comment.