nr: #1 dodano: 2016-12-28 19:12
You are right,
belongs_to is required by default in Rails 5. In your example, you are not running a Rails 5 app, you are just using
belongs_to is required by default in Rails 5 because when you generate a new Rails 5 app, you get a file
config/initializers/new_framework_defaults.rb which has the following line:
# Require `belongs_to` associations by default. Previous versions had false.
Rails.application.config.active_record.belongs_to_required_by_default = true
To achieve similar results in your example, you have to set this config option for
ActiveRecord as well (by default it's not set, which means it's
nil, aka falsy value):
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.belongs_to_required_by_default = true # <-- This line :)
Now if you run your tests, they will pass.
For more info, reference this pull request.