Rails 4: Difference between validates presence on id or association

[亡魂溺海] 提交于 2019-12-01 03:37:23

Investigating further, I found that the 'presence' validator resolves to 'add_on_blank':

http://apidock.com/rails/ActiveModel/Errors/add_on_blank

def add_on_blank(attributes, options = {})
  Array(attributes).each do |attribute|
    value = @base.send(:read_attribute_for_validation, attribute)
    add(attribute, :blank, options) if value.blank?
  end
end

This does what it says: adds a validation error if the property in question is blank?

This means it's simply an existence check. So if I validate an id, that id has to exist. That means:

topping.pancake = Pancake.new
topping.valid?

would return false. However:

topping.pancake_id = -12
topping.valid?

would return true. On the other hand, if I validate the object the exact opposite would be true. Unless -12 is a valid index, in which case ActiveRecord would automatically load it from the database on receipt of the 'pancake' message.

Moving on to my issue, further investigation showed that blank? delegates to empty?, and indeed someone had defined empty? on the pancake, returning true if there are no toppings.

Culprit found, and something about Rails learned.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!