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

There are cases where default value is not set in STI since Rails 8.0.1 #100

Open
k-tsuchiya-jp opened this issue Dec 26, 2024 · 1 comment · May be fixed by #101
Open

There are cases where default value is not set in STI since Rails 8.0.1 #100

k-tsuchiya-jp opened this issue Dec 26, 2024 · 1 comment · May be fixed by #101

Comments

@k-tsuchiya-jp
Copy link

k-tsuchiya-jp commented Dec 26, 2024

Steps to reproduce

$ rails _8.0.1_ new hoge_app
$ cd hoge_app
$ rails g model vehicle type:string color:string price:integer release_date:datetime
$ rails g model car --parent=Vehicle
$ rails db:migrate
# Gemfile
source "https://rubygems.org"

gem "rails", "~> 8.0.1"
gem "default_value_for"

# app/models/vehicle.rb
class Vehicle < ApplicationRecord
  default_value_for :color, "black"
end

# app/models/car.rb
class Car < Vehicle
  default_value_for :price, 100
  default_value_for :release_date, DateTime.now
end
$ bundle install
$ rails r 'p Car.new'
#<Car id: nil, type: "Car", color: "black", price: nil, release_date: "2024-12-26 04:30:36.002660000 +0000", created_at: nil, updated_at: nil>

Expected behavior

$ rails r 'p Car.new'
#<Car id: nil, type: "Car", color: "black", price: 100, release_date: "2024-12-26 04:30:36.002660000 +0000", created_at: nil, updated_at: nil>

Actual behavior

  • price is set to nil

System configuration

Rails version: 8.0.1
Ruby version: 3.3.5
DefaultValueFor version: 4.1.0

Additional information

It seems to be caused by the modification of class_attribute method in Rails 8.0.1.
rails/rails#53640

@k-tsuchiya-jp
Copy link
Author

Before Rails 8.0.1, inherited singleton methods were included in singleton_methods(false), but starting from Rails 8.0.1, inherited singleton methods are no longer included in singleton_methods(false).
As a result, init_hash is always set to true, causing only one attribute in the child class to have its default value set.

else
init_hash = !singleton_methods(false).include?(:_default_attribute_values)
end
if init_hash
self._default_attribute_values = {}
self._default_attribute_values_not_allowing_nil = []
end

k-tsuchiya-jp pushed a commit to k-tsuchiya-jp/default_value_for that referenced this issue Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant