[英]How to define boolean field for a rails migration
I want to add a boolean value field ("is_public") to the table "my_model". 我想在表“my_model”中添加一个布尔值字段(“is_public”)。 Currently I can use this: 目前我可以使用这个:
class AddPublicToDream < ActiveRecord::Migration
def self.up
add_column :my_model, :is_public, :string
end
def self.down
remove_column :my_model, :is_public, :string
end
end
Then I can assign "true" or "false" to mymodel.is_public in controllers. 然后我可以在控制器中为mymodel.is_public分配“true”或“false”。
Can I substitute :string with :boolean to achieve the same effect? 我可以替换:string with:boolean来实现相同的效果吗? Would it save some database space comparing to :string? 与以下字符串相比,它会节省一些数据库空间吗?
是的,您可以使用:boolean
为此,是的,它也将节省数据库空间。
Change the type attribute to :boolean
and run rake db:migrate
again. 将type属性更改为:boolean
并再次运行rake db:migrate
。 You should be able to call, for example: 你应该可以打电话,例如:
Dream.is_public? # returning true or false depending whether is set.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.