Skip to content

Commit

Permalink
Ensure that save on child object fails for invalid belongs_to associa…
Browse files Browse the repository at this point in the history
…tion. Closes #11555. [rubyruy]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9247 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
lifo committed Apr 10, 2008
1 parent 95fdc82 commit c67e985
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Ensure that save on child object fails for invalid belongs_to association. Closes #11555. [rubyruy]

* Add support for interleaving migrations by storing which migrations have run in the new schema_migrations table. Closes #11493 [jordi]

* ActiveRecord::Base#sum defaults to 0 if no rows are returned. Closes #11550 [kamal]
Expand Down
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -922,6 +922,8 @@ def belongs_to(association_id, options = {})
)
end

add_single_associated_save_callbacks(reflection.name)

configure_dependency_for_belongs_to(reflection)
end

Expand Down
Expand Up @@ -377,4 +377,16 @@ def test_cant_save_readonly_association
assert companies(:first_client).readonly_firm.readonly?
end

def test_save_fails_for_invalid_belongs_to
log = AuditLog.new
assert log.valid?

log.build_developer # Build invalid association
assert !log.developer.valid?
assert !log.valid?
assert_equal "is invalid", log.errors.on("developer")

assert !log.save
end

end

0 comments on commit c67e985

Please sign in to comment.