adding multipe new columns in rails migration using block - ruby-on-rails

Is there any better way to add new columns to rails table than this way
class AddColumnsToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :first_name, :string
add_column :users, :last_name, :string
add_column :users, :contact1, :integer
add_column :users, :contact2, :integer
add_column :users, :contact3, :decimal
add_column :users, :contact4, :integer
add_column :users, :contact5, :integer
add_column :users, :contact6, :string
add_column :users, :contact7, :integer
add_column :users, :contact8, :integer
add_column :users, :contact9, :integer
end
end
Can we use change_table method and write these inside a block? instead of repeating the add_column again and again

You can add multiple columns to a same table like this
def change
change_table :users do |t|
t.string :first_name
t.string :last_name
end
end

If you just want to dry, then you can write in following manner also,
{
string: [:first_name, :last_name, :contact6],
integer: [:contact1 ,:contact2 ,:contact4 ,:contact5 ,:contact7 ,:contact8 ,:contact9],
decimal: [:contact3]
}.each do |type, columns|
columns.each { |col| add_column :users, col, type }
end

Related

"rails db:migrate" error : relation "users" does not exist?

I am newbie with rails and here is my problem.
I created a very simple rails program and in the db > migrate > 2023.._add_columns_to_user.rb file, I added this code to this file
class AddColumnsToUser < ActiveRecord::Migration[7.0]
def change
add_column :users, :full_name, :string
add_column :users, :from, :string
add_column :users, :about, :text
add_column :users, :language, :string
add_column :users, :status, :boolean
add_column :users, :status, :boolean, default: false
end
end
Then I ran this code
rails db:migrate
But it gave me this error
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "users" does not exist
May be I must create the database first?
Could you please give me some advices for this problem?
Here is all of my code, if you need for reference.
https://github.com/nguyencuc2586/Addcustomfieldsoutusermodel
Thank you in advance.
You can't alter a table that hasn't yet been created. Your migration should be wrapped in a table defintion:
class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :full_name # shorthand for add_column :users, :full_name, :string
t.string :from
t.text :about
t.boolean :status, default: false # why on earth is this a boolean?
t.timestamps
end
end
end
This results in CREATE TABLE users (...) while your migration would result in ALTER TABLE users (...).

Rails db migrate aborted error when changing a field name after 1st migrate

I had named the add_column :users, :confirmed_at(confirmation_at), :datetime by accident. So I changed it to the appropriate field name :(confirmed_at)
then I entered : rake db:rollback in command line. it was aborted for some reason with the message below however first my Rails db migrate file code:
class AddConfirmableToDevise < ActiveRecord::Migration
def up
add_column :users, :confirmation_token, :string
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_sent_at, :datetime
add_index :users, :confirmation_token, unique: true
end
def down
remove_column :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
end
end
Error after migration:
tzurch:~/workspace (gravitar) $ rake db:migrate:redo
#the error that came up below
_________________________________________________
== 20160902201448 AddFullnameToUser: reverting ================================
-- remove_column(:users, :fullname, :string)
-> 0.0207s
== 20160902201448 AddFullnameToUser: reverted (0.0284s) =======================
== 20160902201448 AddFullnameToUser: migrating ================================
-- add_column(:users, :fullname, :string)
-> 0.0008s
== 20160902201448 AddFullnameToUser: migrated (0.0009s) =======================
== 20160913221959 AddConfirmableToDevise: migrating ===========================
-- add_column(:users, :confirmation_token, :string)
-> 0.0009s
-- add_column(:users, :confirmed_at, :datetime)
-> 0.0005s
-- add_column(:users, :confirmation_sent_at, :datetime)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: duplicate column name: confirmation_sent_at: ALTER TABLE "users" ADD "confirmation_sent_at" datetime:
The problem here is the syntax you are using to remove column is wrong.
Correct syntax is remove_column
remove_column(table_name, column_name, type = nil, options = {})
Change down migration with
class AddConfirmableToDevise < ActiveRecord::Migration
def up
add_column :users, :confirmation_token, :string
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_sent_at, :datetime
add_index :users, :confirmation_token, unique: true
end
def down
remove_column :users, :confirmation_token
remove_column :users, :confirmed_at
remove_column :users, :confirmation_sent_at
remove_index :users, :confirmation_token
end
end
Or remove the down migration altogether and replace up with change, rails will figure out how to rollback:
class AddConfirmableToDevise < ActiveRecord::Migration
def change
add_column :users, :confirmation_token, :string
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_sent_at, :datetime
add_index :users, :confirmation_token, unique: true
end
end
I suppose you have already have confirmation_sent_at field in this table in db

Changing column names in a migration file?

class AddTimestampsToPosts < ActiveRecord::Migration
def change
add_column :posts, :create_up, :datetime
add_column :posts, :update_at, :datetime
end
end
I need to correct :create_up and :update_at to :created_at and :updated_at
How can I achieve this?
THank you!
def change
rename_column :posts, :create_up, :created_at
rename_column :posts, :update_at, :updated_at
end
The ActiveRecord::Migration documentation lists the available transformations you can use.

Rails Paperclip plugin stopped working?

I am using the Paperclip plugin to manage file uploads to my application. For some reason in the last day or so the plugin/model has stopped working and now returns the following error message:
Paperclip::PaperclipError in DeliversController#create
Asset model missing required attr_accessor for 'data_file_name'
As far as I am aware, I haven't touched the delivers controller or the paperclip plugin.
Has anyone seen this error before, or know how I can trace the last change on the file thats error'ing?
For reference the db schema is as follows:
# Create Delivers Table
create_table :delivers do |t|
t.column :caseref, :string
t.column :casesubject, :string
t.column :description, :text
t.column :document_file_name, :string
t.column :document_content_type, :string
t.column :document_file_size, :integer
t.column :document_updated_at, :datetime
t.timestamps
end
# Create Assets Table
create_table :assets do |t|
t.column :attachable_id, :integer
t.column :attachable_type, :string
t.column :date_file_name, :string
t.column :date_content_type, :string
t.column :date_file_size, :integer
t.column :attachings_count, :integer, :default => 0
t.column :created_at, :datetime
t.column :date_updated_at, :datetime
t.timestamps
end
and the asset model is as follows:
class Asset < ActiveRecord::Base
has_attached_file :data,
:url => "/assets/:id",
:path => ":rails_root/assets/docs/:id/:style/:basename.:extension"
belongs_to :attachable, :polymorphic => true
def url(*args)
data.url(*args)
end
def name
data_file_name
end
def content_type
data_content_type
end
def file_size
data_file_size
end
end
Thanks,
Danny
# Create Assets Table
t.column :date_file_name, :string
^^^
class Asset < ActiveRecord::Base
has_attached_file :data,
^^^
See the difference? Once it is datE and than it's datA
Just try changing this
#Create Assets Table
create_table :assets do |t|
t.column :attachable_id, :integer
t.column :attachable_type, :string
t.column :date_file_name, :string
t.column :date_content_type, :string
t.column :date_file_size, :integer
t.column :attachings_count, :integer, :default => 0
t.column :created_at, :datetime
t.column :date_updated_at, :datetime
t.timestamps
end
to this
# Create Assets Table
create_table :assets do |t|
t.column :attachable_id, :integer
t.column :attachable_type, :string
t.column :data_file_name, :string
t.column :data_content_type, :string
t.column :data_file_size, :integer
t.column :attachings_count, :integer, :default => 0
t.column :created_at, :datetime
t.column :date_updated_at, :datetime
t.timestamps
end
I think the error message indicates it as
Asset model missing required attr_accessor for 'data_file_name'

Ruby on Rails DB Migration Script Using Text instead of :string varchar

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

Resources