Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 将db列设为可选或非必需,以便我可以创建记录_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails 将db列设为可选或非必需,以便我可以创建记录

Ruby on rails 将db列设为可选或非必需,以便我可以创建记录,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我在一个表上有一个列,在创建新记录时,该列可能会被填充,也可能不会被填充。我们将其称为免费配送\金额及其类型整数,它位于配送\方法表中 我还有一个表单,它由这个ShippingMethods模型支持。所以我不想让这个字段显示为0,因为它不是零,只是一无所有 如何使该列成为可选列、非必需列、非零列或空字符串,因为它是整数。似乎nil和null不起作用 我尝试了以下迁移,但都没有成功: change_column :shipping_methods, :free_shipping_amount, :

我在一个表上有一个列,在创建新记录时,该列可能会被填充,也可能不会被填充。我们将其称为
免费配送\金额
及其类型
整数
,它位于
配送\方法
表中

我还有一个表单,它由这个
ShippingMethods
模型支持。所以我不想让这个字段显示为
0
,因为它不是零,只是一无所有

如何使该列成为可选列、非必需列、非零列或空字符串,因为它是整数。似乎
nil
null
不起作用

我尝试了以下迁移,但都没有成功:

change_column :shipping_methods, :free_shipping_amount, :integer, 
default: ""

change_column_null :shipping_methods, :free_shipping_amount, true

change_column :shipping_methods, :free_shipping_amount, :integer,    
:default => nil

change_column_default(:shipping_methods, :free_shipping_amount, nil)

change_column :shipping_methods, :free_shipping_amount, :integer, null: true
然后我尝试了这个,但是我的模型支持表单显示了
0
,这不是我想要的

change_column_default(:shipping_methods, :free_shipping_amount, 0)
此表上的字段如下所示:

t.integer  "free_shipping_amount"
def shipping_method_params
  params.require(:shipping_method).permit(:free_shipping_amount) 
  #other values removed for brevity
end
@shipping_shop = ShippingMethod.create(shipping_method_params)
问题是,如果不向该字段传递某些内容,我无法将记录保存到此表中

我使用的强参数如下所示:

t.integer  "free_shipping_amount"
def shipping_method_params
  params.require(:shipping_method).permit(:free_shipping_amount) 
  #other values removed for brevity
end
@shipping_shop = ShippingMethod.create(shipping_method_params)
当我这样创作时:

t.integer  "free_shipping_amount"
def shipping_method_params
  params.require(:shipping_method).permit(:free_shipping_amount) 
  #other values removed for brevity
end
@shipping_shop = ShippingMethod.create(shipping_method_params)
不会创建记录,事务将回滚

哦,天哪。。我在模型里有这个

validates :free_shipping_amount, numericality: true

那么,我如何保存此模型而不传入此字段的值??或者我仍然可以验证数值性吗?

@ToddT我做了这个迁移

class AddFieldToShippingMethods < ActiveRecord::Migration
  def change
    add_column :shipping_methods, :free_shippping_amount2, :integer
  end
end
class AddFieldToShippingMethods
还有这个

在迁移中设置默认值不是强制性的


[]的

arghhhh。。我刚发现被劫的是我的模特。。我必须将其添加到验证中才能接受空白值

validates :free_shipping_amount, numericality: {allow_blank: true}

谢谢大家的时间,给我指出了正确的方向

实际上你什么都不用做。默认情况下,该字段将是可选的,除非您对是否存在进行了验证。您可以查看
db/schema.rb
并发布与
免费送货金额相关的行吗?您面临的问题/错误到底是什么?您表示不希望此字段显示为0。您可以向我们显示显示为0的代码吗?整数列可以是零,也可能是零,但显示为0。我更新了问题,尝试回答每个人的问题。该记录无法保存。。当我将一个值传递给该字段时,它会很好地保存..我再次更新。。我在模型中有这样一个:验证:免费装运金额,数字:正确,但我需要它。。怎么办?