Fun with Ruby: Returning in blocks "overwrites" outside return values
return inside a block can replace a method’s own result and skip later code, which can break Rails helpers like capture and lose html_safe? information.
Rails 2: Calling instance_eval on a scope will trigger a database query
Calling instance_eval or instance_exec on a Rails 2 scope loads its records from the database. The issue is fixed in Rails 3+.
How to fix: Session hash does not get updated when using "merge!"
Rails session hashes can ignore merge!, leaving new values invisible or unsaved. update loads the session for writing and avoids the mismatch.
Removing MiniTest warnings from Rails 4 projects
MiniTest deprecation warnings in Rails 4 often come from older test gems using the outdated API; adding minitest can silence one warning, but some require gem updates.
How to silence UTF-8 warnings on Rails 2.3 with Ruby 1.9
Ruby 1.9 triggers UTF-8 regexp warnings in Rails 2.3.16+ from ActiveSupport; a monkey patch in config/initializers suppresses them.
Rails: Disable options of a select field
Rails select helpers can disable specific options by value, preventing choice of unavailable entries in forms.
Nice way to set data attributes when creating elements with Rails helpers
link_to and content_tag accept a :data hash for HTML5 data attributes in Rails 3, simplifying data-foo and data-bam output.
console-for opens a Rails console remotely on a Capistrano deployment target
console-for opens a remote Rails console on a Capistrano deployment target with one command, so staging and other environments can be accessed quickly.
Hack of the day: A reverse for Rails' &:
Ruby has a reverse of Rails' each(&:method) shorthand for calling an object method with each element: each(&object.method(:call)).
Get compiled code of a view template in Rails 4.2
Inspecting the generated Ruby for erb or haml templates helps debug rendering issues and understand what Rails executes. lookup_context.find_template and the template handler return the compiled output code.
postgres_ext: additional Rails bindings for PostgreSQL
Missing PostgreSQL types and query helpers for Rails 4.x make native array, CTE, and network-style filtering easier in ActiveRecord and Arel.
Embed Google Analytics code for some environments only
Google Analytics should run only in production to avoid polluting visitor statistics. Rails environment checks can gate the tracking snippet.
Make "rake notes" learn about Haml, Sass, CoffeeScript, and other file types
rake notes can miss TODOs in Haml, Sass, and CoffeeScript files unless Rails is taught their comment syntax and extensions.
What The Rails Security Issue Means For Your Startup
Two high-severity Ruby on Rails security bugs and a rubygems.org compromise raise remote code execution risk for startups depending on the framework.
Removing ANSI color codes from Rails logs
ANSI color codes in Rails logs clutter less and vim; stripping escape sequences with sed produces readable log output or a colorless file.
Start Rails console or server with debugger
Enable the Ruby debugger in script/console or script/server to inspect methods and local variables during a Rails session.
Don't name columns like counter_cache columns in Rails pre v4.2.4
Rails can mistake ordinary columns for cached association counters and return empty has_many collections when the cached value is wrong.
Freeze (vendor, unpack) a single Ruby gem with and without Bundler
Vendoring a patched Ruby gem in Rails can fail without gem metadata, especially with Bundler. A proper .specification file and :path setup make the local copy usable.
Self-expiring URLs with Apache
Signed URLs for private uploads can expire without routing downloads through Rails, improving speed while reducing the risk of permanent access.
How to use helper methods in a controller
Expose controller logic to views so helper-style methods can be called from templates, with different APIs in Rails 2 and Rails 3+.
Effectively Using Materialized Views in Ruby on Rails · pganalyze
Large SQL queries become hard to maintain as joins, subqueries, and filters pile up. PostgreSQL views and materialized views can encapsulate that complexity in Ruby on Rails.
Use form_for without the enclosing form tag
Need form builder helpers without emitting a surrounding <form> tag, for partial updates such as XHR-driven field changes. Rails fields_for yields the same builder without hidden inputs or authenticity token.
Passenger booting Rails 3 application in wrong environment
Passenger can start a Rails app in the wrong environment unless Apache is configured with the correct RailsEnv or RackEnv setting.
dbconsole in Rails 3 requires the environment as the first argument
Rails 3 dbconsole misreads -p test and opens the development database unless the environment is passed first.