...an alternative to PostgreSQL fulltext search A simple example with a GIN index in Rails for optimizing a ILIKE query
...insert many records is to have a single INSERT statement describing multiple rows. In Rails 6+ you can do so with ActiveRecord::Base.insert_all. This is very fast, but you...
...twentieth in what ordering? The ordering is unknown, unless you specified ORDER BY. In Rails, if you use Record.first or Record.last, it will default to ordering by id.
...Fixtures are handy for development seed data, they can be loaded in development with: rails db:fixtures:load Downsides Less matchers & library support It is harder to mock with minitest...
...use a light terminal theme Improving Diffs for Ruby, RSpec and Cucumber files See Rails developers: Have better context in Git diffs. This will correctly identify the beginning of a...
Adopting legacy Rails apps Talk to your mentor about how we're approaching applications that are either old or abandoned by a different team earlier: Add E2E tests for the...
Rails' ActiveSupport::TimeWithZone objects have both a timezone code and offset, e.g. Thu, 28 Mar 2019 16:00:00 CET +01:00. Ruby's stdlib TZInfo also has time zones...
...chances are you'll end up in the wrong time zone. This is why Rails actually uses a long list of time zone names with have names like "London" or...
...Best practices for writing code comments Read the following chapters from our book Growing Rails Application in Practice: Dealing with fat models Extracting service objects Discuss with your mentor what...
...shell commands inside other bundles. Example outline Consider this setup: my_project/Gemfile # says: gem 'rails', '~> 3.0.0' my_project/foo/Gemfile # says: gem 'rails', '~> 3.2.0' And, just to confirm this, these are the...
...installed Rails versions for each of the bundles: ~/my_project$ bundle show rails .../gems/rails-3.0.20 ~/my_project$ cd foo && bundle show rails .../gems/rails-3.2.13 Now you will usually just use bundle exec to run...
...used to match rows without knowing a secret token: Potential Query Manipulation with Common Rails Practises CVE-2013-3211 MySQL madness and Rails
Adding the asset cache directory to symlinked directories Popular asset managers for Rails are Sprockets and Webpacker. Both keep a cache of already compiled files that we're...
When using Rails to truncate strings, you may end up with strings that are still too long for their container or are not as long as they could be. You...
...your view might be too isolated, since view-specs will mock a lot of rails behavior and render the view independent from the controller-logic. Therefore it will be more...
...application server without configuration Usage Install Passenger Standalone with gem install passenger Inside any Rails project, start Passenger Standalone with passenger start Alternatives You can use Puma. It also supports...
...Probably the best approach is to just convert emails (and usernames) to lowercase within Rails. Popular authentication libraries (like Clearance and Devise) already do this for you...
} start your server as usual, but go to https://localhost:3000 bundle exec rails s Accept the certificate in your browser See also Creating a self-signed certificate for...
...deployed to the servers. The gems of these groups might not be loaded by rails, however, the deployment process will take longer as the gems will be downloaded and installed...
...allows you to log to multiple sinks. You know this behavior from from the rails server command, that both logs to standard out and the log/development.log file.
...development.log file. Here is an example for Sidekiq: Sidekiq.configure_client do |config| if ENV['RAILS_ENV'] == 'development' || ENV['RAILS_ENV'] == 'test' stdout_logger = ActiveSupport::Logger.new(STDOUT) file_logger = ActiveSupport::Logger.new...
To reload a single-item association in Rails 5+, call #reload_ : post.reload_author In older Railses you can say post.author(true...
...excludes quote characters so you can easily paste the secret into a Ruby string Rails can also generate secrets that can be used for secret_key_base: $ bin/rails secret 5693040b90b1b09d0a73364d950f15e3a9604403c8e81c6e5bb8b51d0981ef3bbde15a52bf5df41ac4974675bb8e31d69d03490dc11abd1086c873783d1be607
JavaScript files that can be loaded individually from your application layout. By default your Rails application has a single entrypoint application.js, which should import all other files.
...mind that you may need to migrate your database as Git is unaware of Rails and will not cast any magic. If Git checked out a commit that is working...
...create only the records needed for the test. Learn Factories, not fixtures By default Rails uses global fixtures for its tests. This is a giant world of example data that...
...other gems in the past, but they all work in the same way. Watch Railscasts PRO #158 Factories not Fixtures (revised) for an introduction to factories in general and FactoryBot...
ORDER BY category DESC, created_at; The easiest options to achieve this in Rails ActiveRecord is to use "WHERE ID IN" with User.where(id: Post.distinct_on_user.pluck(:user_id)).order(...)