my User Model looks like:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.string :email
t.timestamps
end
end
def self.down
drop_table :users
end
end
If I wanted add one more :attribute, is it best to create another migration file for adding a new column (see another Stackoverflow thread) or can I just manually add t.string :name_of_new_attribute and then rake db:migrate?
Thanks!
alter table r add A D; where r is the name of an existing relation, A is the name of the attribute to be added, and D is the type of the added attribute.
Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.
Rails Active Record is the Object/Relational Mapping (ORM) layer supplied with Rails. It closely follows the standard ORM model, which is as follows − tables map to classes, rows map to objects and. columns map to object attributes.
Active Model is a library containing various modules used in developing classes that need some features present on Active Record.
The proper way is to create a new migration. In the main rails project folder, run
rails generate migration AddDetailsToUser address:string age:integer
etc...
and then run rake db:migrate
An alternative to this is to edit the original migration file, reset/destroy the database and re-run all migrations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With