Rails: Adding an index after adding column
You can run another migration, just for the index: class AddIndexToTable < ActiveRecord::Migration def change add_index :table, :user_id end end
You can run another migration, just for the index: class AddIndexToTable < ActiveRecord::Migration def change add_index :table, :user_id end end
My list of changes after moving to Rails 5: Place lib dir into app because all code inside app is autoloaded in dev and eager loaded in prod and most importantly is autoreloaded in development so you don’t have to restart server each time you make changes. Remove any require statements pointing to your own … Read more
I find using the be_within default rspec matcher more elegant: expect(@article.updated_at.utc).to be_within(1.second).of Time.now
erb stands for “Embedded RuBy”. A .html.erb or .erb.html file is HTML with Ruby code embedded in; Rails will evaluate the Ruby to add content to the file dynamically, and will output a “pure” HTML file for rendering.
With rails >= 3.0, you can simply use the placeholder option. f.text_field :attr, placeholder: “placeholder text”
For Rails 5+ Initial Definition: If you are defining your Post model table, you can set references, index and foreign_key in one line: t.references :author, index: true, foreign_key: { to_table: :users } Update Existing: If you are adding references to an existing table, you can do this: add_reference :posts, :author, foreign_key: { to_table: :users } … Read more
Echoing Gareth’s comments… your code will not work as written. It should be rewritten this way: def name=(name) write_attribute(:name, name.capitalize) end def name read_attribute(:name).downcase # No test for nil? end
If you are looking for a way to it without SQL you should be able to use delete_all. Post.delete_all or with a criteria Post.delete_all “person_id = 5 AND (category = ‘Something’ OR category = ‘Else’)” See here for more information. The records are deleted without loading them first which makes it very fast but will … Read more
You can do this all in the initial migration/column definition (at least currently in Rails 5): t.references :transferable_as, index: true, foreign_key: {to_table: :courses} t.references :same_as, index: true, foreign_key: {to_table: :courses}
If you installed postresql on your server then just host: localhost to database.yml, I usually throw it in around where it says pool: 5. Otherwise if it’s not localhost definitely tell that app where to find its database. development: adapter: postgresql encoding: unicode database: kickrstack_development host: localhost pool: 5 username: kickrstack password: secret Make sure … Read more