Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test case to illustrate callbacks definitions and HABTM association are order dependent #8674

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -38,6 +38,23 @@ def add_david
end end
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 class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
self.table_name = 'developers' self.table_name = 'developers'
has_and_belongs_to_many :projects, has_and_belongs_to_many :projects,
Expand Down Expand Up @@ -592,6 +609,13 @@ def test_new_with_values_in_collection
assert project.developers.include?(david) assert project.developers.include?(david)
end 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 def test_find_in_association_with_options
developers = projects(:active_record).developers.to_a developers = projects(:active_record).developers.to_a
assert_equal 3, developers.size assert_equal 3, developers.size
Expand Down