validates :something, :confirmation => true & attr_accessor confusion

北城以北 提交于 2019-12-08 20:04:25

Like Frederick said above, the issue is comparing an instance of String with an instance of Integer.

More than likely, here's what you have in your controller:

Thing.new(params[:thing]) # note all these params come in as a string

What's happening is that since #pin is an integer column, you will get the following behaviour:

my_thing = Thing.new
my_thing.pin = "123456"
my_thing.pin  # Will be the integer 123456, the attribute has been auto-cast for you

But since #pin_confirmed is just a regular attribute, not an integer column, here's the weirdness you will see:

my_thing = Thing.new
my_thing.pin_confirmation = "123456"
my_thing.pin_confirmation  # Will be the *string* "123456", the attribute has been set as is

So naturally, in that case, no matter what values you have, since they come in through the "params" hash (which is always a set of strings), you will end up assigning string values to both attributes, but they will be cast to different types.

There are several ways to fix this.

One, you could create #pin_confirmation as an integer column in the database.

The other is you could add an attribute setter for #pin_confirmation of the following form:

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