Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #12359 from arthurnn/inverse_on_callbacks

Make sure inverse_of is visible on the has_many callbacks
Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
commit 8fbc416c80a4da38b825dd9623209a26d726e8e0 1 parent bea5e32
@rafaelfranca rafaelfranca authored
View
4 activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## unreleased ##
+* Callbacks on has_many should access the in memory parent if a inverse_of is set.
+
+ *arthurnn*
+
* Migration dump UUID default functions to schema.rb.
Fixes #10751.
View
1  activerecord/lib/active_record/associations/has_many_association.rb
@@ -32,6 +32,7 @@ def handle_dependency
def insert_record(record, validate = true, raise = false)
set_owner_attributes(record)
+ set_inverse_instance(record)
if raise
record.save!(:validate => validate)
View
7 activerecord/test/cases/associations/has_many_associations_test.rb
@@ -602,6 +602,13 @@ def test_transactions_when_adding_to_new_record
end
end
+ def test_inverse_on_before_validate
+ firm = companies(:first_firm)
+ assert_queries(1) do
+ firm.clients_of_firm << Client.new("name" => "Natural Company")
+ end
+ end
+
def test_new_aliased_to_build
company = companies(:first_firm)
new_client = assert_no_queries { company.clients_of_firm.new("name" => "Another Client") }
View
6 activerecord/test/models/company.rb
@@ -45,7 +45,7 @@ class Firm < Company
has_many :unsorted_clients, :class_name => "Client"
has_many :unsorted_clients_with_symbol, :class_name => :Client
has_many :clients_sorted_desc, -> { order "id DESC" }, :class_name => "Client"
- has_many :clients_of_firm, -> { order "id" }, :foreign_key => "client_of", :class_name => "Client"
+ has_many :clients_of_firm, -> { order "id" }, :foreign_key => "client_of", :class_name => "Client", :inverse_of => :firm
has_many :clients_ordered_by_name, -> { order "name" }, :class_name => "Client"
has_many :unvalidated_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :validate => false
has_many :dependent_clients_of_firm, -> { order "id" }, :foreign_key => "client_of", :class_name => "Client", :dependent => :destroy
@@ -144,6 +144,10 @@ class Client < Company
has_many :accounts, :through => :firm
belongs_to :account
+ validate do
+ firm
+ end
+
class RaisedOnSave < RuntimeError; end
attr_accessor :raise_on_save
before_save do
Please sign in to comment.
Something went wrong with that request. Please try again.