Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5997843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:19:18+00:00 2026-05-23T00:19:18+00:00

I have the following two migrations already in my database: When I created Prices:

  • 0

I have the following two migrations already in my database:

When I created Prices:

class CreatePrices < ActiveRecord::Migration
  def self.up
    create_table :prices do |t|
      t.string :price_name
      t.decimal :price
      t.date :date

      t.timestamps
    end
    # add_index :prices (not added)
  end

  def self.down
    drop_table :prices
  end
end

and when I added a user_id to Prices:

class AddUserIdToPrices < ActiveRecord::Migration
  def self.up
    add_column :prices, :user_id, :integer
  end
  # add_index :user_id (not added)
end

  def self.down
    remove_column :prices, :user_id
  end
end

Is there a way from the command line to add prices and user_id to index? I looked at this question and still was confused on how to go about adding indexes and the parts where I put “not added” seem like they would be error-prone because they were earlier migrations.

My question is, what is the best way for me to add indexing to prices and user_id?

Thank you for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:19:18+00:00Added an answer on May 23, 2026 at 12:19 am

    I think one extra migration fits well:

    class AddIndexes < ActiveRecord::Migration
    
      def self.up
        add_index :prices, :user_id
        add_index :prices, :price
      end
    
      def self.down
        remove_index :prices, :user_id
        remove_index :prices, :price
      end
    
    end
    

    Or you can use change syntax with newer versions of rails, look at DonamiteIsTnt comment for details:

    class AddIndexes < ActiveRecord::Migration
    
      def change
        add_index :prices, :user_id
        add_index :prices, :price
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say, I have the following two classes: class A(object): def __init__(self, i): self.i
I have a ActiveRecord Car model: class Car < ActiveRecord::Base def check_value puts self.load_size
I have the following two classes. class Settings { function __CONSTRUCT() { echo Settings
Let's say I have the following two classes: public class Person { public string
I have following two classes: template <size_t size> class Cont{ public: char charArray[size]; };
I have following two entities public class User { public int UserId { get;
I have the following two Python functions: @classmethod def serialize_dict(cls, d): values = []
I have the following two models: class Position(models.Model): position = models.CharField(max_length=100) class UserProfile(models.Model): user
I have the following two columns: SELECT b.ip_address AS IP ,b.mask AS MASK FROM
Suppose I have the following two strings containing regular expressions. How do I coalesce

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.