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

Fix belongs_to association validation for virtual attributes #200

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kyanagi
Copy link

@kyanagi kyanagi commented Jan 20, 2025

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.

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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant