Belongs_to presence in Rails 5 not working

前端 未结 3 1277
無奈伤痛
無奈伤痛 2021-02-15 16:16

To my knowledge, the new default in Rails 5 requires belongs_to associations to be present. I made a model with this association, but the problem is I don\'t get pr

相关标签:
3条回答
  • 2021-02-15 16:27

    According to the issue re weird behaviour of config belongs_to_required_by_default, it seems like one of your other gems intervenes in ActiveRecord::Base and causes the bug.

    One of workarounds to the issue is to move the line

    config.active_record.belongs_to_required_by_default = true
    

    from initializers directly into application.rb.

    This worked for me smoothly.

    0 讨论(0)
  • 2021-02-15 16:37

    New Rails 5 applications come with a new initializer in

    config/initializers/active_record_belongs_to_required_by_default.rb
    

    If you upgraded a Rails 4 application or created your application with a beta version of Rails 5, then that file might be missing.

    The configuration in that file enables the feature in question:

    # Be sure to restart your server when you modify this file.
    
    # Require `belongs_to` associations by default. This is a new Rails 5.0
    # default, so it is introduced as a configuration option to ensure that apps
    # made on earlier versions of Rails are not affected when upgrading.
    Rails.application.config.active_record.belongs_to_required_by_default = true
    

    Please check how belongs_to_required_by_default is configured in your application.

    0 讨论(0)
  • 2021-02-15 16:39

    I faced with same problem.

    You can move

    config.active_record.belongs_to_required_by_default = false

    to config/environments/needed_environment.rb or to config/application.rb

    Helped for me!

    0 讨论(0)
提交回复
热议问题