了解Rails迁移语句(:null => false) [英] Understanding rails migration statement (:null => false)
问题描述
我试图理解以下声明,它是从Rails迁移文件中获得的:
I am trying to understand the following statement, it is from a rails migration file:
x.datetime "new", :null => false
x.datetime "update", :null => false
我理解这两个语句的第一部分(逗号之前的所有内容),但不确定空白部分
I understand the the first part of both statements (everything before the comma) but I am unsure on the null portion
:null => false
这基本上是在说如果不存在,那就是假的"?逻辑似乎有点奇怪,对此进行任何澄清将大有帮助.
Is this basically saying "if it does not exist, then it is false?" The logic just seems a bit strange, any clarification on this would be greatly helpful.
推荐答案
因为它最初提到了CoffeeScript ,所以我认为问题与语法和翻译有关.为此,请参考 Peter Bloom的答案.
I had taken the question to be about syntax and translation since it originally mentioned CoffeeScript. For purpose, refer to Peter Bloom's answer.
我不确定
:null => false
的确切含义.
=>
运算符是Ruby中的键/值分隔符,定义了 Hash
,且:null
键设置为false
.在CoffeeScript/JavaScript中,Object
文字与:
相似-{ null: false }
.
The =>
operator is a key/value separator in Ruby, defining a Hash
with a :null
key set to false
. It's similar to :
for an Object
literal in CoffeeScript/JavaScript -- { null: false }
.
在参数列表中使用时,它是允许/模仿Ruby中命名参数的一种选择.
When used in an argument list, it's one option for allowing/imitating named arguments in Ruby.
另一个主要区别是CoffeeScript/JavaScript使用String
作为键,而Ruby通常使用(cs/js)与:null
(rb).
The other main difference is that CoffeeScript/JavaScript use String
s for keys while Ruby typically uses Symbols -- "null"
(cs/js) vs. :null
(rb).
因此,CoffeeScript中的语法等效项是:
So, the syntactic equivalent in CoffeeScript would be:
x.datetime "new", null: false
x.datetime "update", null: false
在JavaScript中,这是:
In JavaScript, that's:
x.datetime("new", { null: false });
x.datetime("update", { null: false });
这篇关于了解Rails迁移语句(:null => false)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!