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

Closed
k-tsuchiya-jp opened this issue Dec 26, 2024 · 4 comments · Fixed by #101
Closed

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 · 4 comments · Fixed by #101
Assignees
Labels

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

@jrafanie
Copy link
Collaborator

Thank you @k-tsuchiya-jp for this very good bug report with rails 8. Thank you for the bug report and the proposed solution in #101. I'll review it over there.

@jrafanie
Copy link
Collaborator

jrafanie commented Jan 3, 2025

@k-tsuchiya-jp pushed 4.1.1... thanks again!

@jrafanie jrafanie added the bug label Jan 3, 2025
@jrafanie jrafanie self-assigned this Jan 3, 2025
@k-tsuchiya-jp
Copy link
Author

@jrafanie
Thank you for Merge and Release!
I was on vacation for New Year holidays.
I appreciate your support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants