How do you write a migration to rename an ActiveRecord model and its table in Rails?

Here’s an example: class RenameOldTableToNewTable < ActiveRecord::Migration def self.up rename_table :old_table_name, :new_table_name end def self.down rename_table :new_table_name, :old_table_name end end I had to go and rename the model declaration file manually. Edit: In Rails 3.1 & 4, ActiveRecord::Migration::CommandRecorder knows how to reverse rename_table migrations, so you can do this: class RenameOldTableToNewTable < ActiveRecord::Migration def change … Read more

Rails 4: List of available datatypes

Here are all the Rails 4 (ActiveRecord migration) datatypes: :binary :boolean :date :datetime :decimal :float :integer :bigint :primary_key :references :string :text :time :timestamp Source: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_column These are the same as with Rails 3. If you use PostgreSQL, you can also take advantage of these: :hstore :json :jsonb :array :cidr_address :ip_address :mac_address They are stored as … Read more

How do I handle too long index names in a Ruby on Rails ActiveRecord migration?

Provide the :name option to add_index, e.g.: add_index :studies, [“user_id”, “university_id”, “subject_name_id”, “subject_type_id”], unique: true, name: ‘my_index’ If using the :index option on references in a create_table block, it takes the same options hash as add_index as its value: t.references :long_name, index: { name: :my_index }

In Ruby on Rails, what’s the difference between DateTime, Timestamp, Time and Date?

The difference between different date/time formats in ActiveRecord has little to do with Rails and everything to do with whatever database you’re using. Using MySQL as an example (if for no other reason because it’s most popular), you have DATE, DATETIME, TIME and TIMESTAMP column data types; just as you have CHAR, VARCHAR, FLOAT and … Read more

What is the easiest way to duplicate an activerecord record?

To get a copy, use the dup (or clone for < rails 3.1+) method: #rails >= 3.1 new_record = old_record.dup # rails < 3.1 new_record = old_record.clone Then you can change whichever fields you want. ActiveRecord overrides the built-in Object#clone to give you a new (not saved to the DB) record with an unassigned ID. … Read more

How can I call controller/view helper methods from the console in Ruby on Rails?

To call helpers, use the helper object: $ ./script/console >> helper.number_to_currency(‘123.45′) => “R$ 123,45” If you want to use a helper that’s not included by default (say, because you removed helper :all from ApplicationController), just include the helper. >> include BogusHelper >> helper.bogus => “bogus output” As for dealing with controllers, I quote Nick’s answer: … Read more

How do I make a column unique and index it in a Ruby on Rails migration?

The short answer for old versions of Rails (see other answers for Rails 4+): add_index :table_name, :column_name, unique: true To index multiple columns together, you pass an array of column names instead of a single column name, add_index :table_name, [:column_name_a, :column_name_b], unique: true If you get “index name… is too long”, you can add name: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)