43
loading...
This website collects cookies to deliver better user experience
7.0.0.rc1
, the first release candidate. Basecamp, HEY, Github, and Shopify have all been running the Rails 7 alpha in production, so we can expect even the release candidate to be pretty stable.Webpacker
gem, this brought a lot of baggage, was hard to understand and make any changes to, especially while maintaining upgradability.rails new
is to use import maps through the importmaps-rails
gem. Instead of writing a package.json
and installing dependencies with npm
or yarn
, you use ./bin/importmap
CLI to pin (or unpin or update) dependencies.date-fns
:$ ./bin/importmap pin date-fns
config/importmap.rb
like:pin "date-fns", to: "https://ga.jspm.io/npm:[email protected]/esm/index.js"
import { formatDistance, subDays } from 'date-fns'
formatDistance(subDays(new Date(), 3), new Date(), { addSuffix: true })
//=> "3 days ago"
$ ./bin/rails javascript:install:[esbuild|rollup|webpack]
encrypts
method on ActiveRecord::Base
. This means that after an initial setup, you can write code like this:class Message < ApplicationRecord
encrypts :text
end
deterministic: true
option to the encrypts
method.load_async
method that you can use when querying data to fetch results in the background. This is especially important when you need to load several un-related queries from a controller action. You can run:def PostsController
def index
@posts = Post.load_async
@categories = Category.load_async
end
end
:unlimited
as the attempts
parameter on retry_on
. Rails will continue to attempt the job without any maximum number of attempts.class MyJob < ActiveJob::Base
retry_on(AlwaysRetryException, attempts: :unlimited)
def perform
raise "KABOOM"
end
end
ActiveStorage
instead of specifying size on every access.class User < ApplicationRecord
has_one_attached :avatar do |attachable|
attachable.variant :thumb, resize: "100x100"
end
end
#Call avatar.variant(:thumb) to get a thumb variant of an avatar:
<%= image_tag user.avatar.variant(:thumb) %>
tag.attributes
method for use in views that translates a hash into HTML attributes:<input <%= tag.attributes(type: :text, aria: { label: "Search" }) %>>
<input type="text" aria-label="Search">
byebug
to the debug
gem.byebug
, you now need to call debugger
in the code to enter a debugging session.sole
or find_sole_by
(instead of first
or find_by
) when you want to assert that the query should only match a single record.Product.where(["price = %?", price]).sole
# => ActiveRecord::RecordNotFound (if no Product with given price)
# => #<Product ...> (if one Product with given price)
# => ActiveRecord::SoleRecordExceeded (if more than one Product with given price)
user.api_keys.find_sole_by(key: key)
# as above
where.associated(:association)
to check if an association is present on a record instead of joining and checking for the existence of an id.# Before:
account.users.joins(:contact).where.not(contact_id: nil)
# After:
account.users.where.associated(:contact)
send_stream
inside a controller action to start streaming a file that is being generated on the fly.send_stream(filename: "subscribers.csv") do |stream|
stream.write "email_address,updated_at\n"
@subscribers.find_each do |subscriber|
stream.write "#{subscriber.email_address},#{subscriber.updated_at}\n"
end
end
7.0.0.rc1
as of the publication date) and run bundle update
.bundle exec rails app:update
. Follow the interactive CLI and add/replace/modify the files as required.