Add column to model, ruby on Rails

add category_id column to posts table

rails g migration add_category_id_to_posts category_id:integer

You ought to edit automatically generated migration and add line on the end of change method:

add_index :posts, :category_id
then run : rake db:migrate

----------------------------
if there is aproblem in saving new fields to database 

you must permit it in the controller

like this

def user_posts_params

params.require(:user_posts).permit(:user_id, :category_id)

end

and this in the model 

attr_accessible :user_id, :category_id

 
Advertisements

One thought on “Add column to model, ruby on Rails

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s