Permalink
Browse files

test case to illustrate order dependent callbacks

  • Loading branch information...
1 parent dfc930b commit 4e0c79eec199c2962a0f3d48623c6a1feafa1087 @senny committed Jan 1, 2013
Showing with 24 additions and 0 deletions.
  1. +24 −0 activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -38,6 +38,23 @@ def add_david
end
end
+class ProjectWithAfterCreateHookBeforeHABTM < ActiveRecord::Base
+ self.table_name = 'projects'
+
+ after_create :add_david
+
+ has_and_belongs_to_many :custom_developers,
+ :class_name => "DeveloperForProjectWithAfterCreateHook",
+ :join_table => "developers_projects",
+ :foreign_key => "project_id",
+ :association_foreign_key => "developer_id"
+
+ def add_david
+ david = DeveloperForProjectWithAfterCreateHook.find_by_name('David')
+ custom_developers << david
+ end
+end
+
class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
self.table_name = 'developers'
has_and_belongs_to_many :projects,
@@ -592,6 +609,13 @@ def test_new_with_values_in_collection
assert project.developers.include?(david)
end
+ def test_after_create_before_habtm_definition
+ project = ProjectWithAfterCreateHookBeforeHABTM.create! :name => 'Getting things done'
+ project.save!
+
+ assert_equal 1, project.custom_developers.count
+ end
+
def test_find_in_association_with_options
developers = projects(:active_record).developers.to_a
assert_equal 3, developers.size

0 comments on commit 4e0c79e

Please sign in to comment.