https://github.com/willnet/add_optional_true
https://github.com/willnet/add_optional_true
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/willnet/add_optional_true
- Owner: willnet
- License: mit
- Created: 2023-10-23T08:56:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-05-15T10:03:56.000Z (10 months ago)
- Last Synced: 2025-07-14T02:41:24.548Z (8 months ago)
- Language: Ruby
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AddOptionalTrue
This is a script to add `optional: true` to `belongs_to` in the target files.
In Rails 5.0, a setting called `config.active_record.belongs_to_required_by_default` was introduced. When it's set to `true`, all existing `belongs_to` associations will behave as `optional: false`, and a validation will be added to check if the associated record exists. This might be fine for small applications where the impact is clear. However, for large applications, it's difficult to investigate the full impact. In such cases, adding `optional: true` helps maintain the current behavior of existing `belongs_to` associations, allowing a gradual shift toward the Rails default configuration.
## Usage
```bash
git clone https://github.com/willnet/add_optional_true.git
cd add_optional_true
bundle install
bundle exec ruby-rewrite -l add_optional_true.rb -m your_ruby_file_or_directory
```