11

I am pretty new to Ruby on Rails, and I was wondering if there was a way to edit the database schema for a model.

For example, I have the Subscriber model in my application -- the way I created it was by using rails generate scaffold Subscriber email:string

But now, I want a name in the subscriber model as well. Is there any easy way to do this? I have put a lot of code in my current controllers and views, so I don't necessarily want to destroy the scaffold, but I would like to edit the model.

Thanks in advance,

hwrd

P.S. I am using Ruby on Rails 3

hwrdprkns
  • 7,525
  • 12
  • 48
  • 69
  • 1
    Reading http://guides.rubyonrails.org/active_record_migrations.html should be very helpful. – Hong Mar 25 '16 at 01:40

2 Answers2

15

An ActiveRecord Model inspects the table it represents. You don't actually need to change your model just to add a new field (unless you want to add validations, etc).

What you want to do is make a new migration and then migrate your database up:

rails g migration AddNameToSubscribers name:string
rake db:migrate

Then you can start referencing the name field in your controllers and views.

(This generator command might seem a little magical, but the rails generator recognizes this format and will generate the appropriate add_column and remove_column code. See the Rails migration guide for further reading.)

Josh Lindsey
  • 8,455
  • 3
  • 24
  • 25
  • 1
    But don't the old columns in the DB still take up space? I didn't look at it this way though. Thats a good point. – hwrdprkns Jan 07 '11 at 19:35
  • @hwrdprkns: Yes, they still take up space. I'm not sure what you're asking. This command will add the `name` field to your `subscribers` table. Is that not what you wanted? – Josh Lindsey Jan 07 '11 at 19:38
  • 1
    Old columns haven't been deleted, so they will still take up space. The migration above is only adding a column to the subscribers table, that is all. – Zabba Jan 07 '11 at 19:38
  • Josh, I was asking how I could edit the database schema of a model. I think a combination of you and Jesse's answers is what I wanted. In any case, I'm sure I can play around with these ideas until I can get it to work for me. – hwrdprkns Jan 07 '11 at 19:58
2

If you mean changing the database schema of your model, you'll want to use migrations.

You'll do things like

add_column :city, :string
remove_column :boo

http://guides.rubyonrails.org/migrations.html

If you do only mean finding models and updating the data inside each instance, go with @apneadiving's answer.

Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
  • Thanks for your prompt reply. I mean changing the database schema -- sorry I didn't make that clear. After this command, will my schema be updated for good? Also, as just a side note -- does remove_column also bash any/all data in the column for that model? – hwrdprkns Jan 07 '11 at 19:31
  • @hwrdprkns, when a column is removed from a table, all data for that is lost. And what @Jesse Wolgamott is saying is essentially the same thing as what @Josh Lindsey said.. – Zabba Jan 07 '11 at 19:37
  • Sorry I voted this down, I didn't mean to! If you edit your answer I'll vote it back up! – hwrdprkns Jan 07 '11 at 19:59