Back

rails - belongs_to 在最新版本默认是 required .

发布时间: 2022-02-20 00:54:00

refer to: https://www.bigbinary.com/blog/rails-5-makes-belong-to-association-required-by-default

在目前的版本中(貌似 >= Rails 5? ) 只要你写了belongs_to , 那么默认该列就是必须的.

例如

class Apple
   belongs_to :fruit
end

那么在 a = Apple.new  ,  a.save! 的时候,就会报错:  fruit 不能是空. 
解决办法:

1. 要么设置好fruit 
2. 要么 这样:

belongs_to :fruit, optional: true 

要么: (不建议)

 Rails.application.config.active_record.belongs_to_required_by_default = true

Back