如何为 Rails 迁移定义布尔字段
我想向表“my_model”添加一个布尔字段(“is_public”)。目前,我可以使用这个:
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
然后我可以在控制器中为 mymodel.is_public 分配“true”或“false”。
我可以用 :boolean 替换 :string 来达到相同的效果吗?与 :string 相比,它会节省一些数据库空间吗?
I want to add a boolean field ("is_public") to the table "my_model". 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.
Can I substitute :string with :boolean to achieve the same effect? Would it save some database space compared to :string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以使用
:boolean
来实现此目的,是的,它还会节省数据库空间。Yes, you can use
:boolean
for this, and yes it will also save database space.将 type 属性更改为
:boolean
并再次运行rake db:migrate
。您应该能够调用,例如:Change the type attribute to
:boolean
and runrake db:migrate
again. You should be able to call, for example: