开发者

How to define boolean field for a rails migration

I want to add a boolean value 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 comparing to :string?


Yes, you can use :boolean for this, and yes it will also save database space.


Change the type attribute to :boolean and run rake db:migrate again. You should be able to call, for example:

Dream.is_public?  # returning true or false depending whether is set.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜