RSpec 1 (Rails 2) With the most recent spec_candy.rb helpers you can say: User.stub_any_instance(:foo => :bar) user = User.new user.foo # => :bar RSpec 2 (Rails 3) RSpec 2 comes with...
[mysqld] sql_mode="" Now restart MySQL: sudo service mysql restart For a single Rails project If you only want to change the SQL mode for one project, you can...
...with the following snippet: actors = movie.actors actors.build actors.unshift(actors.pop(1)) # won't work with Rails 4+ Because build_for_form creates new objects and appends them to the one-to...
...many association collection object you only have to reorder the collection objects. Sorting with Rails 3+ = form.fields_for :children, @parent.children.sort_by(&:name) do |fieldsform...
...behaves like an unordered Hash, but is much faster. Info If you're using Rails you already have concurrent-ruby in your bundle. hamster hamster provides several collection classes, such...
There is a practical short list for valid/invalid example email addresses - Thanks to Florian L.! The definition for valid emails...
...false (intercepts invocation), whereas false&.class permits the invocation. &. might also remind you of Rails' Object#try. However, their scopes are different, and in Rails 4 nil-checking is only...
...add ANSI color annotations (which your console then interprets). To print a log (e.g. rails log) and color all lines containing "FATAL" in red and all lines with "INFO" in...
...often times the results of such subtractions go into a view. When upgrading a Rails application from Ruby 1.8 to 1.9, this will cause significant pain, and under most circumstances...
...cache_or_resolve :var, :some_expensive_calculation end This is what "memoize" in earlier rails versions did. There is a gem that reintroduces this behavior, but this is the essential...
...for your application's bit of metaprogramming. It may matter for a framework like Rails, and for methods that are called really often, but that is not your everyday code...
...for when you can use transactions: Transactions cannot be used for Selenium features, where Rails and test run in different processes and thus don't see changes within the transaction...
...different cleaning strategies I measured the runtime of different strategies using an average-sized Rails project (with MySQL): Cucumber RSpec Transaction 87.14, 86.65 10.20, 10.11
Issue: You have an app using jsbundling-rails and esbuild. After deploy, the assets built by esbuild are missing in public/assets. Solution: Add app/builds to your git repo (by adding...
Rails has the handy controller method send_file which lets us download files easily. We can decide whether the file should be downloaded (disposition: 'attachment') or shown in the browser...
...use it anyway, learn the lessons we already learnt: Use a compatible Rubygems for Rails 2.3 on Ruby 1.8.7 Ruby 1.8.7 is not compatible with current Rubygems versions (> 2.0). Runnig...
...Keep in mind that this will not affect processes that are invoked through the Rails console. Put the environment variables into the user's shell configuration (e.g., ~/.bashrc) if you...
...column" ASC NULLS FIRST Your indexes will have to specify this as well. In Rails, declare them using add_index :table, :column, order: { column: 'DESC NULLS LAST' } Multiple columns
...need to represent later time values, use the DateTime class. This is also what Rails does when it loads a record from the database that has a DATETIME value which...
ActiveModel::Errors is used to handle validation errors on Rails objects. If you inspect an instance, it feels like a hash (to be more precise, it inherits from Hash):
...49/65) travis-lint... Slow requires: 110.21 render_anywhere 147.33 nokogiri 173.83 haml 179.62 sass-rails 205.04 delayed_job_active_record 286.76 rails 289.36 mail 291.98 capistrano 326.05 delayed_job...
...Otherwise add database_cleaner to your *Gemfile`. This lets you say this from the Rails console: DatabaseCleaner.strategy = :truncation DatabaseCleaner.clean You can package this into a nice little Rake task by...
...can e.g. clean the test database by saying on the shell: rake db:clean RAILS_ENV=test If you are using the parallel_tests gem, the task is aware and...
...example to confirm your output in tests), you can use roo. To easily dump Rails records to XLSX, there is also axlsx_rails (haven't tried it yet). xlsx generation...
If possible, make the autoloaded class static by moving it out of Rails' load path...
...it's usually not part of the Gem's API). Unless you're using Rails 4 (which brings String#indent in ActiveSupport), you'll be best of defining it yourself...
...data in columns. This is helpful for e.g. comparing attributes of a set of Rails records. def tp(objects, *method_names) terminal_width = `tput cols`.to_i cols = objects.count...