-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix belongs_to association validation for virtual attributes
Previously, `belongs_to` associations in `ActiveType::Object` did not properly validate presence when `ActiveRecord::Base.belongs_to_required_by_default = true` and `ActiveRecord.belongs_to_required_validates_foreign_key = false` (Rails default). ``` require "bundler/inline" gemfile do source "https://rubygems.org" gem "activerecord" gem "sqlite3" gem "active_type" end require "active_record" require "sqlite3" require "active_type" ActiveRecord::Base.belongs_to_required_by_default = true ActiveRecord.belongs_to_required_validates_foreign_key = false ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") ActiveRecord::Schema.define do create_table :shops do |t| end create_table :items do |t| t.references :shop, null: false, foreign_key: true end end class Shop < ActiveRecord::Base end class Item < ActiveRecord::Base belongs_to :shop end class X < ActiveType::Object attribute :shop_id, :integer belongs_to :shop end p Item.new.valid? #=> false p Item.new(shop_id: -1).valid? #=> false p X.new.valid? #=> false p X.new(shop_id: -1).valid? #=> true ``` This is because the `validates_presence_of` validation uses `ActiveType::Object#attribute_changed?` and it returns always `false` for virtual attributes. ``` p X.new(shop_id: 1).attribute_changed?(:shop_id) #=> false ``` https://github.com/rails/rails/blob/v8.0.1/activerecord/lib/active_record/associations/builder/belongs_to.rb#L135 This commit implements `ActiveType::VirtualAttributes#attribute_changed?` and fixes the problem. The same applies to `ActiveType::Record`.
- Loading branch information
Showing
5 changed files
with
204 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters