使用rails scaffold命令时是否可以为属性分配默认值?怎么样?

时间:2016-02-04 07:54:25

标签: ruby-on-rails scaffolding

例如,我想生成一个带有布尔字段的脚手架,默认为false

2 个答案:

答案 0 :(得分:5)

不,你不能从命令行那样做。

您可以阅读有关可从命令行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

答案 1 :(得分:2)

是的,你可以尝试给出长度,但不能给出默认值

rails g scaffold users firstname:string{50}