EXCITING NEWS! 🤩
Summer is here, and with it comes plenty of free time to dive deep into
#Ruby
and
#RubyOnRails
!
That's why we are releasing several new courses, products, and videos, in addition to our recurring new resources ✨💫
Enjoy! 💚
🔗
✨ RAILS PRO TIPS ✨
Use ActiveRecord::FinderMethods
#sole
to guarantee a single record result in your ActiveRecord queries ✨💫
#rubyonrails
#activerecord
✨ RAILS PRO TIPS ✨
Take advantage of ActiveRecord's association extensions to add methods that are only used as part of this association. ✨💫
#rubyonrails
✨ RAILS PRO TIPS ✨
Take advantage of the all_day() method to return a Range representing the whole day of the current time ✨💫
Also available:
➡️ all_month()
➡️ all_year()
#rubyonrails
#activesupport
✨ RAILS PRO TIPS ✨
Don't bother yourself writing an email regexp!
Take advantage of the built-in Ruby constant URI::MailTo::EMAIL_REGEXP instead! 🤩
#ruby
#rubyonrails
✨ ActiveRecord Pro Tips ✨
Did you know that Rails 7 now supports virtual columns?
Take advantage of this feature to create virtual columns calculated by the database during INSERT and UPDATE ✨💫
#rubyonrails
✨ RAILS PRO TIPS ✨
Use the variants: keyword argument to effortlessly select the correct view based on your current_user's role. 🚀
For more PRO TIPS: 🔗
#rubyonrails
✨ RAILS PRO TIPS ✨
Extract complex filtering logic from your controllers by using custom filter classes✨💫
For more PRO Tips → 🔗
#rubyonrails
#railscallbacks
✨ RAILS PRO TIPS ✨
Use ActiveSupport::ErrorReporter to capture and report errors to your preferred service ✨💫
Some error-reporting libraries such as Sentry and HoneyBadger automatically register a subscriber for you!
#rubyonrails
✨ RAILS PRO TIPS ✨
Use ActiveRecord::FinderMethods
#sole
to guarantee a single record result in your ActiveRecord queries! ✨💫
#activerecord
#rubyonrails
✨ RAILS PRO TIPS ✨
Dive into the elegance of ActiveRecord's missing method! 😍
Say goodbye to complex queries and hello to simplicity. 👋
#rubyonrails
#ActiveRecord
✨ RAILS PRO TIPS ✨
Take advantage of
➡️ Object
#presence
➡️ and the McCarthy evaluation
to elegantly select the first non-blank value ✨💫
#rubyonrails
#programming
✨ RAILS PRO TIPS ✨
Use the variants: keyword argument to effortlessly select the correct view based on your current_user's role. 🚀
For more PRO TIPS: 🔗
#rubyonrails
✨ RAILS PRO TIPS ✨
Did you know that you can use constraints with regular expressions to validate dynamic segments in routes? ✨💫
#railsrouting
#rubyonrails
✨ RAILS PRO TIPS ✨
Did you know that Rails 7 now supports virtual columns?
Take advantage of this feature to create virtual columns calculated by the database during INSERT and UPDATE ✨💫
Subscribe to RubyCademy PRO for a detailed explanation 😉
#activerecord
#rubyonrails
✨ RAILS PRO TIPS ✨
Take advantage of ActiveRecord's association extensions to add methods that are only used as part of this association. ✨💫
Here is a useful example if you need to share the same extension between many associations. 🤯
#ActiveRecord
#RubyOnRails
✨ RAILS PRO TIPS ✨
Use ActiveSupport::ErrorReporter to capture and report errors to your preferred service ✨💫
Some error-reporting libraries like
➡️
@getsentry
➡️
@honeybadgerapp
➡️
@AppSignal
automatically register a subscriber for you! 🚀
#rubyonrails
🛤️ New on Rails 7.1 🛤️
The Rails 7.1 API includes the following asynchronous methods:
➡️ async_count
➡️ async_sum
➡️ async_minimum
➡️ async_maximum
➡️ async_average
➡️ async_pluck
➡️ async_ids
➡️ async_find_by_sql
➡️ async_count_by_sql
#rubyonrails
✨ RAILS PRO TIPS ✨
You don't want to use a gem just to add a layer of presentation logic to your Rails app? 🤔
So take advantage of delegate_missing_to to implement a robust Presenter pattern in your app! ✨💫
FOR MORE PRO TIPS:
#rubyonrails
Take advantage of the becomes method to select the partial template to render on the fly! ✨💫
PS: The becomes method creates a new object, discarding initially eager-loaded associations, leading to potential N+1 query issues.
#rubyonrails
#activerecord
#actionview
Take advantage of ActiveRecord::Relation
#many
? to verify if your query contains more than one record at an SQL level ✨💫
To learn more on Rails 🔗
#rubyonrails
#activerecord
Did you know that the ActiveRecord API includes the following asynchronous methods? ✨💫
➡️ async_count
➡️ async_sum
➡️ async_minimum
➡️ async_maximum
➡️ async_average
➡️ async_pluck
➡️ async_ids
➡️ async_find_by_sql
➡️ async_count_by_sql
#rubyonrails
#activerecord
🚨 𝗥𝗔𝗜𝗟𝗦 𝗣𝗥𝗢 𝗧𝗜𝗣𝗦 🚨
Did you know that you can use the file: options to extract your Ruby version from a version file in your Gemfile?
#rubyonrails
#bundler
✨ RAILS PRO TIPS ✨
Take advantage of the dependent: destroy_async option to delete the associated collection in a background job 🤯 🤩
💡 Make sure that foreign_key: false is set in your migration
➡️ A detailed course on this topic will be added this week exclusively on
✨ RAILS PRO TIPS ✨
Take advantage of ActiveRecord::Relation
#many
? to verify if your query contains more than one record at an SQL level ✨💫
#rubyonrails
#activerecord
✨ RAILS PRO TIPS ✨
Take advantage of accessed_fields and the after_action callback to optimize your high-traffic pages by selecting only the required fields ✨💫
#rubyonrails
#rails
#activerecord
✨ RAILS TIPS ✨
Take advantage of the db:system:change task to change the database.yml file and your database gem to the target database ✨💫
FOR MORE PRO TIPS: 🔗
#activerecord
#rubyonrails
🕹️ RAILS CONSOLE TIPS 🕹️
Make your custom
➡️ instance variables
➡️methods
➡️ and aliases
instantly accessible in the Rails Console! ✨💫
#rubyonrails
#railsconsole
✨ RAILS PRO TIPS ✨
Rails 7 modified the behavior of transaction blocks. ✨💫
Now, early block termination triggers a rollback instead of committing the transaction!
#rubyonrails
#activerecord
✨ RAILS PRO TIPS ✨
Did you know that when ActiveRecord::RecordInvalid is raised, it internally saves the record that caused this error? ✨💫
#RubyOnRails
🚨🚨
Use install_if for conditionally installing your gems! 💎
If the proc passed to install_if returns:
➡️ true: the gem included in the bundle and added to the lockfile.
➡️ false: the gem is not included in the bundle but added to the lockfile
#gemfile
#ruby
#rubyonrails
🎉 We’ve reached 2,500 followers! 🎉
A huge thank you to each and every one of you for your support and enthusiasm for
#Ruby
and
#RubyOnRuby
.
We feel so blessed to be a part of this amazing community! 💚
Let’s continue to learn and grow together! 😊
The RubyCademy Team 💻
🎉🎂 RubyCademy turns 8 today! 🎂🎉
A massive THANK YOU to all of you for these 8 incredible years of learning, coding, and growing together.
Your support means the world! 💚
PS: We've got a gift for you. Stay tuned! It will be available soon. 😉
#RubyCademy
#RubyOnRails
What do Gitlab, Discourse, and Homebrew have in common? 🤔
They use the timeout library provided by the Ruby Core team to elegantly handle potential timeouts in their code base! ✨💫
#rubyonrails
✨ RAILS PRO TIPS ✨
Did you know that when ActiveRecord::RecordInvalid is raised, it internally saves the record that caused this error?
#RubyOnRails
#ActiveRecord
✨ RAILS PRO TIPS ✨
Here is another example using the extending method.
This is particularly useful if you need to share the same extension between many associations. 🤯
#RubyOnRails
#ActiveRecord
✨ RAILS PRO TIPS ✨
Take advantage of ActiveRecord's association extensions to add methods that are only used as part of this association. ✨💫
#rubyonrails
✨ FREE SAMPLE FROM RUBYCADEMY PRO ✨
Take advantage of the `make --jobs N` option to speed up your `bundle install` significantly ✨💫
For more PRO Tips →
#bundler
#rubyonrails
🛤️ New on Rails 7 📷
Rails 7 modified the behavior of transaction blocks.
Now, early block termination triggers a rollback instead of committing the transaction!
#RubyOnRails
#ActiveRecord
✨ RAILS PRO TIPS ✨
You don't want to use a gem to add a layer of presentation logic to your Rails app?
So take advantage 𝗠𝗼𝗱𝘂𝗹𝗲#𝗱𝗲𝗹𝗲𝗴𝗮𝘁𝗲_𝘁𝗼_𝗺𝗶𝘀𝘀𝗶𝗻𝗴 to seamlessly implement a robust Decorator/Presenter pattern in your app! ✨💫
#RubyOnRails
✨ RAILS PRO TIPS ✨
Take advantage of ActiveModel::API to implement models similar to ActiveRecord::Base ✨💫
PS: Note that ActiveModel::Model is nothing but an empty shell that includes ActiveModel::API.
✨ RAILS PRO TIPS ✨
Did you know that Rails provides an easy way to separate your routes into multiple files? 🤔
Indeed, take advantage of the draw method to effortlessly organize your routes! ✨💫
#rubyonrails
#railsrouting
Remember, 𝒄𝒐𝒅𝒆 𝒊𝒔 𝒓𝒆𝒂𝒅 𝒎𝒐𝒓𝒆 𝒐𝒇𝒕𝒆𝒏 𝒕𝒉𝒂𝒏 𝒊𝒕'𝒔 𝒘𝒓𝒊𝒕𝒕𝒆𝒏.
Invest time in choosing clear and intuitive method names to make your
#Ruby
code a pleasure to read and maintain.
Here is a reminder of some of the most popular naming conventions