user3624831
user3624831

Reputation: 205

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

Upvotes: 15

Views: 21491

Answers (2)

Surya
Surya

Reputation: 16002

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

Upvotes: 21

Anand
Anand

Reputation: 3760

You should use rails generate migration AddFeaturedToMyModelName featured:boolean to get the migration to add to The Model called MyModelName. If the rake db:migrate did include a migration that looked like ..._add_featured.rb, then do rake db:rollback STEP=1 before generating the correct migration and then call rake db:migrate

Upvotes: 2

Related Questions