ruby on rails - Adding/Updating column in a Model using RubyMine -
i'm working on web app interacts db table through form. had make changes resulted in me adding column db table. i'm using rubymine developing , can't figure out how update model contains new column. i've added inside model so:
class student < activerecord::base attr_accessible :f_name, :floor_pref, :l_name, :is_active validates_presence_of :f_name, :floor_pref, :l_name end i've added inside schema.rb:
activerecord::schema.define(:version => 20130620140537) create_table "students", :force => true |t| t.string "f_name" t.string "l_name" t.string "floor_pref" t.boolean "is_active" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end end and inside migrate so:
class createstudents < activerecord::migration def change create_table :students |t| t.string :f_name t.string :l_name t.string :floor_pref t.boolean :is_active t.timestamps end end end problem when run rails console , model object doesn't show new column have added (:is_active). shows old columns:
>> student loading development environment (rails 3.2.13) switch inspect mode. student(id: integer, f_name: string, l_name: string, floor_pref: string, created_at: datetime, updated_at: datetime) what missing?
what should know, delete changes have added model app/models/student.rb , db/schema.rb
then should go terminal/console , type
$ rails g migration add_column_name_to_student column_name:type this command create new file in db/migrate/ folder.
$ rake db:migrate after updating database schema rake db:migrate. go app/models/student.rb , add new column_name attr_accessible, if want setup value form.
this whole procedure has nothing ide.
here other resources, can information how add new column models.
generate rails migrations automagically add change rails 3: how add new field existing database table
you should avoid adding new columns way did that. it's messy , makes code difficult maintain.
Comments
Post a Comment