I have seen two different ways of migrating a database. Which one is the proper way to do it in Rails 3?
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :title
t.timestamps
end
end
and
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.column :name, :string
t.timestamps
end
end
Thank You!
t.string :title is just a shortcut for t.column :title, :string
Both of them are ok, there is no discrimination. I'd normally prefer the short form, as it is more readable to me but it's just a matter of opinion.
Related
I am trying to list/order my columns in a custom order with active scaffold, Rails 5.1.2 and Ruby 2.3.3p222.
Migration as follows:
class CreateResources < ActiveRecord::Migration[5.1]
def change
create_table :resources do |t|
t.string :firstname
t.string :surname
t.date :date_of_birth
t.string :identity_number
t.string :nationality
t.string :state_province
t.date :interview_date
t.string :available
t.string :home_phone
t.string :mobile_phone
t.timestamps
end
end
end
And I have added columns, the migration is as follows:
class AddColumnsToResources < ActiveRecord::Migration[5.1]
def change
add_column :resources, :expected_salary, :string
add_column :resources, :current_ctc, :string
add_column :resources, :years_of_experience, :string
add_column :resources, :notice_period, :string
end
end
I then used paperclip to add an attachment to the table:
class AddAttachmentAttachmentToResources < ActiveRecord::Migration[5.1]
def self.up
change_table :resources do |t|
t.attachment :attachment
end
end
def self.down
remove_attachment :resources, :attachment
end
end
I need to sort my columns so they appear in the following order:
firstame
surname
identity number
...
Since I am new to Rails, I am following this 'getting started' guide on rails website.
On section 6.1 Generating a Model, I should run rails generate model Comment commenter:string body:text post:references to get a comment model.
Supposedly, this is what should be included in the migration file:
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
t.timestamps
end
add_index :comments, :post_id
end
end
But in my migration file, I have everything but this line add_index :comments, :post_id.
Instead, I have index:true following the t.references :post
I can't seem to find an explanation to this, can anyone explain to me what is going on here? Because later on I need to use :post_id, but in my version of migration, it is not clearly declared. I am very confused.
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
t.timestamps
end
add_index :comments, :post_id
end
end
and
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post, index: true
t.timestamps
end
end
end
will do same things, both of them will add index to post_id column
also you can later add index to any column any column that you want to add to your model like
$ rails generate migration AddPartNumberToProducts part_number:string:index
that will produce this code:
class AddPartNumberToProducts < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
add_index :products, :part_number
end
end
in above code if you want to create index for post_id then write it as t.integer :post_id
else you can try this add_index :comments, :post
I'm having this table
class CreateEvents < ActiveRecord::Migration
def self.up
create_table :events do |t|
t.integer :subcategory
t.string :event_name
t.text :description
t.string :location
t.date :date
t.decimal :price
t.timestamps
end
end
def self.down
drop_table :events
end
end
and i want to change the subcategory to subcategory_id. I tries this one but is not working
ruby script/generate migration RenameDatabaseColumn and then i went to the file which is in db\migrate and edited to look like this
class RenameDatabaseColumn < ActiveRecord::Migration
def self.up
rename_column :events, :subgategory, :subgategory_id
end
def self.down
# rename back if you need or do something else or do nothing
end
end
then i run the command rake db:migrate put the column is still subcategory. Can you help me please? I'm using rails 2.0
Thank you
Did you misspell the column name? isn't it :subcategory? You wrote :subgategory.
class RenameDatabaseColumn < ActiveRecord::Migration
def self.up
rename_column :events, :subcategory, :subcategory_id
end
def self.down
# rename back if you need or do something else or do nothing
end
end
I'm diving into RoR and I need to remove a model and its table, as well as update the other models that reference it. I did a search on google and SO and the best answer I found was this, but the answer is unclear to me. The final consensus was to use the ruby script/destroy model method and then "manually edit any migrations that might contain refs to these deleted models" It's this last part that I'm unclear about. I want to delete the models for my user and profile models and tables...
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :email
t.string :password
t.timestamps
end
end
def self.down
drop_table :users
end
end
class CreateProfiles < ActiveRecord::Migration
def self.up
create_table :profiles do |t|
t.string :name
t.integer :user_id
t.timestamps
end
end
def self.down
drop_table :profiles
end
end
and update the article model and table that references them...
class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.string :title
t.text :body
t.datetime :published_at
t.string :image
t.timestamps
end
end
def self.down
drop_table :articles
end
end
class AddUserIdToArticles < ActiveRecord::Migration
def self.up
add_column :articles, :user_id, :integer
end
def self.down
remove_column :articles, :user_id
end
end
Can I just do ruby script/destroy user and then call the self.down methods in the article migrations? If so, how do I call the 'self.down` methods and in what order?
Thanks so much in advance for your help!
Yep. Just delete it with
ruby script/destroy model user
ruby script/destroy model profile
And then rollback your database, or self.down with this :
rake db:rollback
Now you can safely delete your migration file.
I'm a beginner at Ruby on Rails so I apologize if this is quite obvious, but I'm trying to learn how to write the database migration scripts and I'd like to change the following long_description to a text value instead of string:
class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.column "short_description", :string
t.column "long_description", :string
t.timestamps
end
end
end
Any ideas how this is possible?
class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.string :short_description
t.text :long_description
t.timestamps
end
end
def self.down
# don't forget the down method
end
end
Also, don't forget the down method.
Migration types are listed here.
:string
:text
:integer
:float
:decimal
:datetime
:timestamp
:time
:date
:binary
:boolean
create_table :articles do |t|
t.column 'long_description', :text
# ...
end
Set it to :text
Here's a good ref for you: Here