Adding/Updating column in a Model using RubyMine

倖福魔咒の 提交于 2019-12-13 04:23:59

问题


I'm working on a web app that basically interacts with a db table through a form. Recently I had to make some changes which resulted in me adding another column to the db table. I'm using RubyMine for developing and can't figure out how to update the model so that it contains the new column. I've added it inside the model as 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 also added it inside the schema.rb:

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

create_table "students", :force => true do |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 also inside the migrate as so:

class CreateStudents < ActiveRecord::Migration
def change
create_table :students do |t|
  t.string :f_name
  t.string :l_name
  t.string :floor_pref
  t.boolean :is_active

  t.timestamps
end
end
end

Problem is when I run the rails console and look up my model object it doesn't show the new column that I have added (:is_active). It only shows the old columns:

>> Student
Loading development environment (Rails 3.2.13)
Switch to inspect mode.
Student(id: integer, f_name: string, l_name: string, floor_pref: string, created_at:    datetime, updated_at: datetime)

What am I missing?


回答1:


What you should do know, is to delete all changes you have added to model app/models/Student.rb and to db/schema.rb

Then you should go to your terminal/console and type

$ rails g migration add_column_name_to_student column_name:type

This command will create new file in db/migrate/ folder.

$ rake db:migrate

After updating your database schema with rake db:migrate. Go to your app/models/Student.rb and add new column_name into attr_accessible, if you want to setup this value by form.

This whole procedure has nothing to do with your IDE.

Here are some other resources, where you can get information how to add new column to models.

Generate Rails Migrations That Automagically Add Your Change Rails 3: How to add a new field to an existing database table

You should definitely avoid adding new columns the way you did that. It's messy and makes your code difficult to maintain.




回答2:


This is not the correct way to add a new column to your table. You want to open your console and run the following command your after is rails g migration add_column_name_to_table column_name:type then run a bundle exec rake rake db:migrate. Its not good to just shove things in.




回答3:


Ex: If I want to add the postedon column to posts table, then:

rails g migration AddPostedOnToPosts  posted_on:date

This will add column in your migration file automatically.



来源:https://stackoverflow.com/questions/17728201/adding-updating-column-in-a-model-using-rubymine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!