There are multiple ways to redirect URLs to a different URL in Rails, and they differ in small but important nuances. Imagine you want to redirect the following url...
end But how to solve the uniqueness problem? Another day, another undocumented Rails feature! This time, it’s that ActiveRecord::Base.connection.add_index supports an undocumented option to pass...
(excerpt from: Fancy Postgres indexes with ActiveRecord) So regarding to the cited site Rails 5+ allows me to use an SQL statement in my index: t.index 'shop_id, lower...
When deploying Rails applications you might have noticed that JS and CSS are not cached by all browsers. In order to force Apache to add expiry dates to its response...
...when I change a file? Changed stylesheets and javascripts will always be reloaded because Rails appends a screen.css?1234567 timestamp to the paths. Background images referred to from the CSS...
In Ruby on Rails ActiveRecord::Relation#merge overwrites existing conditions on the same column. This may cause the relation to select more records than expected: authorized_users = User.where(id...
...collectiveidea.com/blog/archives/2012/01/27/testing-file-downloads-with-capybara-and-chromedriver module DownloadHelpers TIMEOUT = 10 module_function def download_path download_path = Rails.root.join("tmp/test_downloads#{ENV['TEST_ENV_NUMBER']}") FileUtils.mkdir_p(download_path) download_path end def clear_downloads FileUtils.rm...
When your JavaScript bundle is so massive that you cannot load it all up front, I would recommend to load...
...This could be your (very careless) controller method: def generated send_file File.join(Rails.root, 'shared', 'invoices', params[:number]) end This allows your users not only to access those files but...
...directory of the files as the first parameter, like so: send_file_inside File.join(Rails.root, 'shared', 'invoices'), params[:number] Do not use only Rails.root -- this would allow access to config/environment.rb...
...when your application uses time zones. Background A time-zoned Time attribute on a Rails record is converted to UTC using to_s(:db) to be stored, and converted back...
...without changing the return value: def save_user user.save.tap do |saved| next unless saved Rails.log("User was created, we have #{user.count} users now!") end end save_user # User was created...
...several gems that help to you do that, like Sidekiq or Resque. With newer Rails you can also use ActiveJob as interface for a background processing library. See here for...
end and a features/support/active_job.rb with: # Jobs should be worked off immediately in tests Rails.application.config.active_job.queue_adapter = :inline
...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...
...bar']) => /tmp/foo20220912-14561-3g93n1bar You can choose a different base directory than Dir.tmpdir e.g. Dir.mktmpdir('foo', Rails.root.join('tmp')) => /home/user/rails_example/tmp/foo20220912-14561-pyr8qd. This might be necessary when your tests are running on CI. For this...
...something with a temporary directory' do # ... end Option 4: Without Dir.mktmpdir Use something like "#{Rails.env}#{ENV['TEST_ENV_NUMBER']}" in you directory path when using Dir.mkdir. Further reading:
When I sign in as "carcar79" Patch to make it work for Rails 2 For Rails 2, the last line of find_by_anything needs to be changed...
options: { search: 'window.toString() === \'[object GjsGlobal]\'', replace: 'window.toString() === \'[object Window]\'' } }] }) Fix for ESBuild / jsbundling-rails Add the package esbuild-plugin-text-replace to your package.json. Now add the following plugin...
...webpack = require('webpack') environment.plugins.prepend('Provide', new webpack.ProvidePlugin({ getJasmineRequireObj: ['spec/support/jasmine_provider.js', 'default'], })) Fix for ESBuild / jsbundling-rails
Returning an empty scope can come in handy, e.g. as a default object. In Rails 4 you can achieve this by calling none on your ActiveRecord model. MyModel.none # returns an...
...empty ActiveRecord::Relation object For older Rails versions you can use the attached initializer to get a none scope...
...ids on an ActiveRecord scope to pluck all the ids of the relation: # Modern Rails User.where("users.name LIKE 'Foo Bar'").ids # Rails 3.2+ equivalent User.where("users.name LIKE 'Foo Bar'").pluck...
# Edge rider equivalent for Rails 2+ User.where("users.name LIKE 'Foo Bar'").collect_ids
Params are tricky Testing for request params is a little tricky because Rails hides details about how HTTP works. In particular GET requests encode their params in the URL...
...up releases, not assets. In order to clean up assets, you can use capistrano-rails. Just require 'capistrano/rails' in Capfile and add the following config: # config/deploy.rb set :keep_assets...
...remove any older versions. If you are using Webpacker, you need to configure capistrano-rails. If you are on Rails 3, you cannot use this to clean up assets. :keep...
...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...
When a user shares your content, a snippet with title, image, link and description appears in her timeline. By default...
...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.
...used to match rows without knowing a secret token: Potential Query Manipulation with Common Rails Practises CVE-2013-3211 MySQL madness and Rails
...after_logout idp_sign_out] end Unsafe redirect when trying to log out Since Rails 7 you need to pass allow_other_host: true to redirect_to to allow a...
...a month or a year. Next, start your application server for HTTPS. For a Rails application with Puma: bin/rails server -b 'ssl://0.0.0.0:3000?key=development.key&cert=development.crt'