rails generate migration command to insert data into the table - ruby-on-rails

I have a table and I had to add a migration script to add rows in the table.
Please help with the rails generate migration command to insert data into the table.
Thanks,
Ramya.

You can write regular ruby code inside a migration. So you can simply do something like this:
class Foo < ActiveRecord::Migration
def self.up
User.create(:username => "Hello", :role => "Admin")
end
def self.down
User.delete_all(:username => "Hello")
end
end
Just write regular ruby inside your migration same as you would in pry or rails console.

The code helped me is the sql statement as show
In migration file
def up
execute("insert into salary_ranges(salary_range) values('Above 2000');")
end

class AddFieldInUsers < ActiveRecord::Migration
def self.up
add_column :users, :admin, :boolean, :null => false, :default => 0
end
def self.down
remove_column :users
end
end

Related

How can I change column type on rails 5

I insert a column in my sqlite with a wrong type "stringimage".
How can I change the column type to string?
I tried change_column :users, :uid, :string
and
def up
change_table :users do |t|
t.change :uid, :stringimage
end
end
def down
change_table :users do |t|
t.change :uid, :string
end
end
but it doesn't works.
I tried many things but none of it works, maybe because I'm using rails 5.
You Need to write following two definitions into your migration :
def up
change_column :my_table, :my_column, :string
end
def down
change_column :my_table, :my_column, :stringimage
end
Note that change_column is an irreversible migration. It will cause an error if you try to rollback. To prevent this, modify the usual change method in the migration to use two separate up and down methods like this:
class ChangeUsersUidType < ActiveRecord::Migration
def up
change_column :users, :uid, :string
end
def down
change_column :users, :uid, :stringimage
end
end
If you liked this answer you can read more in this article: https://kolosek.com/rails-change-database-column.
You can try this:
change_column(table_name, column_name, type, options): Changes the column to a different type using the same parameters as add_column.

Undefined method 'add_reference'

I'm trying to add a user reference to my post tables with following code:
class AddUserIdToPosts < ActiveRecord::Migration
def change
add_reference :posts, :user, index: true
end
end
but I've received an error message:
undefined method 'add_reference'
Anyone knows how to solve this?
I'm using Rails 3.2.13
In Rails 3 you must do it like so
class AddUserIdToPosts < ActiveRecord::Migration
def change
add_column :posts, :user_id, :integer
add_index :posts, :user_id
end
end
Only in Rails 4 you can do it the way you posted.
add_reference is specific to rails 4.0.0, so you should try this instead :
class AddUserIdToPosts < ActiveRecord::Migration
def change
add_column :posts, :user_id, :integer
add_index :posts, :user_id
end
end
this is a great post about this subject
Your migration should be
rails generate migration AddUserRefToPosts user:references
How about this:
def change
change_table :posts do |p|
p.references :user, index: true
end
end
This method apperead in Rails 4.0
I think you may create some monkey patch with this functionality for Rails 3.2

How can I add index, and reindex to the existing attribute?

I'm fetching a record by the code just like these
#community = Community.find_by_community_name(params[:community_name])
#user = User.find_by_username(params[:username])
I want to make it faster loading, so I'm thinking of adding index to them just like this.
If I do rake db:migrate, does it reindex to the existing records also?
Or just the records that will be created from now on?
Do it improve the speed of loading by adding index just like this?
class AddIndexToCommunity < ActiveRecord::Migration
def self.up
add_index :communities, [:community_name, :title, :body], :name=>:communities_idx
end
def self.down
remove_index :communities, [:community_name, :title, :body], :name=>:communities_idx
end
end
class AddIndexToUser < ActiveRecord::Migration
def self.up
add_index :users, [:username, :nickname, :body], :name=>:users_idx
end
def self.down
remove_index :users, [:username, :nickname, :body], :name=>:users_idx
end
end
rake db:migrate will perform database migrations and apply indeces immediately.You should apply indeces only to columns which you use in searching. Remember that indeces add time penalty on insert and update operations. If you load records on by their names, add indeces only to names:
class AddIndexToCommunity < ActiveRecord::Migration
def self.up
add_index :communities, :community_name
end
def self.down
remove_index :communities, :community_name
end
end
class AddIndexToUser < ActiveRecord::Migration
def self.up
add_index :users, :username
end
def self.down
remove_index :users, :username
end
end

rails migration copy and remove table

I have user model and user model has_one profile model.
Also I have user.phone and user.profile.phone but I want to remove user.phone and I will use only user.profile.phone.
Before I remove the user.phone,I wanna copy user.phone to user.profile.phone if user.phone is not blank.Then I will remove user.phone
For instance:
user.phone = 123
user.profile.phone = 234
After migration:
user.phone will be removed
user.profile.phone = 123 - 234
What is the appropriate migration for this purpose?
try this
class YourMigration < ActiveRecord::Migration
def self.up
User.find_each do |user|
user.profile.update_attributes(:phone => user.phone) unless user.phone.blank?
end
remove_column :users, :phone
end
def self.down
add_column :users, :phone, :string
end
end
If your database is not very large you can simply do like this:
User.includes(:profile).all.each{ |u| u.profile.phone = u.phone unless u.phone.nil? }
in your console. Or you can write smth like this in your migration:
def change
User.includes(:profile).all.each{ |u| u.profile.phone = u.phone unless u.phone.nil? }
remove_column :users, :phone
end
class YourMigration < ActiveRecord::Migration
def self.up
User.where("phone IS NOT NULL").includes(:profiles).each{ |u| u.profile.phone = u.phone}
remove_column :users, :phone
end
def self.down
add_column :users, :phone, :string
end
end
I prefer not to use Model in migration because it creates unnecessary pain:
Assume many people working on same project and you use model in migration do commit. Other person delete the user model or applies some validation on model and dos the commit. When he or other tries to run the migrations, it may fail because the model you used is not exists or some validation.
So I recommend to use SQL statements in migration.
class SomeMigartion < ActiveRecord::Migration
def self.up
execute('update profiles p inner join users u on p.user_id = u.id set p.phone = u.phone where u.phone is not null')
remove_column :users, :phone
end
def self.down
add_coulmn :users, :phone
end
end

Active Record query

I have two models ForumThread and Post set-up like this:
class ForumThread < ActiveRecord::Cached
has_many :posts
end
class Post < ActiveRecord::Cached
end
class CreateForumThreads < ActiveRecord::Migration
def self.up
create_table :forum_threads do |t|
t.column :thread_name, :text
end
add_index :forum_threads, :thread_name
end
def self.down
drop_table :forum_threads
end
end
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.column :post_body, :text
t.integer :forum_thread_id, :null => false
t.integer :priority
end
end
def self.down
drop_table :posts
end
end
I'd like to create a query that returns all forum threads where there's at least one post in each thread with priority of one. How do I create this query?
I've been considering something like ForumThread.joins(:posts).select(:priority => 1). I'm relatively new to Active Record (and totally new to joins) so any help is appreciated.
ForumThread.joins(:posts).where(:posts => {:priority => 1})
see join with conditions
First of all you should rename thread_id field to forum_thread_id in posts table and add posts_count to forum_threads table.
In Post class add belongs_to :forum_thread, :counter_cache => true
Now you can query ForumThread.where("posts_count > ?", 1).joins(:posts).where("posts.priority = ?", 1) which will return you a collection of posts.

Resources