How can I specify a local gem in my Gemfile?
I believe you can do this: gem “foo”, path: “/path/to/foo”
I believe you can do this: gem “foo”, path: “/path/to/foo”
You want to call .name on the object’s class: result.class.name
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
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
There are several issues with each of the available methods, but I believe that defining an after_initialize callback is the way to go for the following reasons: default_scope will initialize values for new models, but then that will become the scope on which you find the model. If you just want to initialize some numbers … Read more
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 }
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
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
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
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