From 3af4ae82e587b12664626e7b22bc6cc21ebbca2e Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 25 Sep 2013 14:24:27 -0400 Subject: Make sure inverse_of is visible on the has_many callbacks --- activerecord/test/cases/associations/has_many_associations_test.rb | 7 +++++++ activerecord/test/models/company.rb | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index ebeead0dc2..fd9b129ed4 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -518,6 +518,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase 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") } diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index 8104c607b5..1aa77f95bf 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -39,7 +39,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 @@ -117,6 +117,10 @@ class Client < Company has_many :accounts, :through => :firm, :source => :accounts belongs_to :account + validate do + firm + end + class RaisedOnSave < RuntimeError; end attr_accessor :raise_on_save before_save do -- cgit v1.2.3