In Rails 5 belongs to associations are required by default. For some reason, after adding delayed_job_active_record to my Gemfile the presence of associated records was no longer required.
Here's how this can be reproduced:
- Generate a new rails app with rails 5.0.0.rc1.
- Generate 2 models that share a relationship:
class MyModel < ApplicationRecord
belongs_to :my_related_model # This should imply a presence validation in Rails 5
end
class MyRelatedModel < ApplicationRecord
has_many :my_models
end
- Check that the presence of a
MyRelatedModel is required for a valid MyModel
m = MyModel.new
m.valid? #=> false
m.errors.full_messages #=> ["My related model must exist"]
- Add
delayed_job_active_record to the Gemfile and run through the installation instructions in the delayed_job README
gem "delayed_job_active_record"
$ bundle install
$ rails generate delayed_job:active_record
$ rake db:migrate
- Check that the presence of a
MyRelatedModel is required for a valid MyModel
m = MyModel.new
m.valid? #=> true
m.errors.full_messages #=> []
In Rails 5 belongs to associations are required by default. For some reason, after adding
delayed_job_active_recordto my Gemfile the presence of associated records was no longer required.Here's how this can be reproduced:
MyRelatedModelis required for a validMyModeldelayed_job_active_recordto the Gemfile and run through the installation instructions in thedelayed_jobREADMEMyRelatedModelis required for a validMyModel