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
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
...
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 have one table called profiles with some columns.
Now I wish to add a few columns to this table using the change-method in rails 3.1. I created a migration with the following code:
def change
change_table :profiles do |t|
t.string :photo
t.string :name
t.references :user
end
end
The migration works perfectly, but when I want to rollback I get
SQLite3::SQLException: duplicate column name: photo: ALTER TABLE "profiles" ADD "photo" varchar(255)
Any ideas why?
The auto-generated migrations for adding columns in Rails 3.1 is in the format:
class AddColumnToTable < ActiveRecord::Migration
def change
add_column :table, :column, :type
end
end
Perhaps try that syntax?
It looks like you need to tell the migration how to revert itself:
def change
change_table :profiles do |t|
t.string :photo
t.string :name
t.references :user
end
reversible do |dir|
dir.down do
remove_column :profiles, :photo
remove_column :profiles, :name
remove_column :profiles, :user_id
end
end
end
See http://guides.rubyonrails.org/migrations.html#using-reversible for more information.
Alternatively you could try using the old up and down methods which are still available like so:
def up
change_table :profiles do |t|
t.string :photo
t.string :name
t.references :user
end
end
def down
remove_column :profiles, :photo
remove_column :profiles, :name
remove_column :profiles, :user_id
end
More on up/down here: http://guides.rubyonrails.org/migrations.html#using-the-up-down-methods
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.
I created a model ruby script/generate model Article (simple enuff)
Here is the migration file create_articles.rb:
def self.up
create_table :articles do |t|
t.column :user_id, :integer
t.column :title, :string
t.column :synopsis, :text, :limit => 1000
t.column :body, :text, :limit => 20000
t.column :published, :boolean, :default => false
t.column :created_at, :datetime
t.column :updated_at, :datetime
t.column :published_at, :datetime
t.column :category_id, :integer
end
def self.down
drop_table :articles
end
end
When I run the rake:db migrate command I receive an error rake aborted! "Uninitialized constant CreateArticles."
Does anyone know why this error keeps happening?
Be sure that your file name and class name say the same thing(except the class name is camel cased).The contents of your migration file should look something like this, simplified them a bit too:
#20090106022023_create_articles.rb
class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.belongs_to :user, :category
t.string :title
t.text :synopsis, :limit => 1000
t.text :body, :limit => 20000
t.boolean :published, :default => false
t.datetime :published_at
t.timestamps
end
end
def self.down
drop_table :articles
end
end
It's possible to get the given error if your class names don't match inflections (like acronyms) from config/initializers/inflections.rb.
For example, if your inflections include:
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'DOG'
end
then you might need to make sure the class in your migration is:
class CreateDOGHouses < ActiveRecord::Migration[5.0]
rather than:
class CreateDogHouses < ActiveRecord::Migration[5.0]
Not super common, but if you generate a migration or a model or something, and then add part of it to inflections afterwards it may happen. (The example here will cause NameError: uninitialized constant CreateDOGHouses if your class name is CreateDogHouses, at least with Rails 5.)
If you're getting this error and it's NOT because of the migration file name, there is another possible solution. Open the class directly in the migration like this:
class SomeClass < ActiveRecord::Base; end
It should now be possible to use SomeClass within the migration.
The top answer solved for me. Just leaving this here in case it helps.
Example
If your migration file is called
20210213040840_add_first_initial_only_to_users.rb
then the class name in your migration file should be
AddFirstInitialOnlyToUsers
Note: if the class name doesn't match, it will error even if the difference is just a lower case t instead of an upper case 'T' in 'To' - so be careful of that!