使用 rails 脚手架命令时是否可以为属性分配默认值?如何?
Is it possible to assign default values to attributes when using rails scaffold command? How?
例如,我想生成一个带有默认为 false
的布尔字段的脚手架
是的,您可以尝试给出长度但无法给出默认值的一种方法
rails g scaffold users firstname:string{50}
不,您不能从命令行执行此操作。
您可以阅读有关从命令行传递的可用修饰符 in docs。
还不错,很实用(有点旧但还是有用的)article。
您至少可以做以下事情:
# Specify the type of the field
rails g model user email age:integer
# Add index for column
rails g model user email:index location_id:integer:index
# Add uniq index for column
rails g model user pseudo:string:uniq
# Set limit for field of integer, string, text and binary fields:
rails generate model user pseudo:string{30}
# generate decimal field with scale and precision:
rails generate model product 'price:decimal{10,2}'
# You can combine any single curly brace option with the index options:
rails generate model user username:string{30}:uniq
# options to generate reference columns (fields which are used in rails as foreign keys):
rails generate model photo album:references
例如,我想生成一个带有默认为 false
是的,您可以尝试给出长度但无法给出默认值的一种方法
rails g scaffold users firstname:string{50}
不,您不能从命令行执行此操作。
您可以阅读有关从命令行传递的可用修饰符 in docs。
还不错,很实用(有点旧但还是有用的)article。
您至少可以做以下事情:
# Specify the type of the field
rails g model user email age:integer
# Add index for column
rails g model user email:index location_id:integer:index
# Add uniq index for column
rails g model user pseudo:string:uniq
# Set limit for field of integer, string, text and binary fields:
rails generate model user pseudo:string{30}
# generate decimal field with scale and precision:
rails generate model product 'price:decimal{10,2}'
# You can combine any single curly brace option with the index options:
rails generate model user username:string{30}:uniq
# options to generate reference columns (fields which are used in rails as foreign keys):
rails generate model photo album:references