Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Boolean Column Value in Ruby on Rails

I am working on a Ruby on Rails project that creates a web blog. I am looking to add a boolean database field called featured to the Post model. This field should be editable via the active admin interface that I have added.

I used the following code, but I don't even get another column showing up on the website.

$rails generate migration addFeatured featured:boolean
$rake db:migrate

I am very new to Ruby on Rails and would appreciate any help.

The relevant code in my index.html.erb file(views):

<th>Featured Post</th>
<td><%= post.featured %></td>

Schema.rb:

ActiveRecord::Schema.define(:version => 20141126015126) do

create_table "active_admin_comments", :force => true do |t|
 t.string   "namespace"
 t.text     "body"
 t.string   "resource_id",   :null => false
 t.string   "resource_type", :null => false
 t.integer  "author_id"
 t.string   "author_type"
 t.datetime "created_at",    :null => false
 t.datetime "updated_at",    :null => false
end

add_index "active_admin_comments", ["author_type", "author_id"], :name =>    "index_active_admin_comments_on_author_type_and_author_id"
add_index "active_admin_comments", ["namespace"], :name => "index_active_admin_comments_on_namespace"
add_index "active_admin_comments", ["resource_type", "resource_id"], :name => "index_active_admin_comments_on_resource_type_and_resource_id"

create_table "admin_users", :force => true do |t|
 t.string   "email",                  :default => "", :null => false
 t.string   "encrypted_password",     :default => "", :null => false
 t.string   "reset_password_token"
 t.datetime "reset_password_sent_at"
 t.datetime "remember_created_at"
 t.integer  "sign_in_count",          :default => 0
 t.datetime "current_sign_in_at"
 t.datetime "last_sign_in_at"
 t.string   "current_sign_in_ip"
 t.string   "last_sign_in_ip"
 t.datetime "created_at",                             :null => false
 t.datetime "updated_at",                             :null => false
end

add_index "admin_users", ["email"], :name => "index_admin_users_on_email", :unique => true
add_index "admin_users", ["reset_password_token"], :name => "index_admin_users_on_reset_password_token", :unique => true

create_table "comments", :force => true do |t|
 t.string   "commenter"
 t.text     "body"
 t.integer  "post_id"
 t.datetime "created_at", :null => false
 t.datetime "updated_at", :null => false
end

add_index "comments", ["post_id"], :name => "index_comments_on_post_id"

create_table "posts", :force => true do |t|
 t.string   "name"
 t.string   "title"
 t.text     "content"
 t.datetime "created_at", :null => false
 t.datetime "updated_at", :null => false
end

end
like image 943
user3624831 Avatar asked Nov 26 '14 01:11

user3624831


People also ask

How do I set default value in migration rails?

In Ruby on Rails, you can set default values for attributes in the database by including them as part of your migration. The syntax is default: 'value' . This is useful if you want to define lots of attributes at once, and it's easy to see what the default value is at a glance when looking at your db/schema. rb file.

How do you rename a column in Ruby on Rails?

You can use the change method with the rename_column definition in your migrations files to update a single column name in your database.


1 Answers

You need to do:

$ rails g migration AddFeaturedToPosts featured:boolean

If you already have this migration file created with name something like: 201411......_ add_featured.rb then first rollback:

$ rake db:rollback STEP=1 // or use VERSION=201411....

modify it:

add_column :posts, :featured, :boolean

and then:

$ rake db:migrate
like image 155
Surya Avatar answered Oct 21 '22 08:10

Surya