Fix Rubygems binary error: undefined method `activate_bin_path' for Gem:Module (NoMethodError)
So you're getting an error like this:
undefined method `activate_bin_path' for Gem:Module (NoMethodError)
Here is what happened:
- You installed a recent version of Rubygems
- You installed some gems that install a binary (like
bundle
,rake
orrails
) with code that only works with modern Rubygems versions - You downgraded Rubygems to an older versions, which doesn't change any binaries
- When calling binaries with the old Rubygems version, it cannot process the line
Gem.activate_pin_path(...)
that was written out by th...
Things to consider when using Travis CI
Travis CI is a free continuous integration testing service. However, it is really fragile and will break more than it will work.
If you choose to 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 rvm rubygems latest-1.8 --force
will fix this and install Rubygems version 1.8.29.
To make Travis CI do this, add `before_script: rvm rubygems latest-1....
Yarn: How to recognize that you are using a different node version than your colleagues
The issue in this card can occur if the node_modules
directory is checked into your Git repository. We usually recommend to exclude node_modules
from version control.
In any case you should document which version of node to use in your project in a .nvmrc
file.
I saw a strange behaviour after we introduced webpack in one of our projects and finally found out the reason: The person who committed the files used a node version that is older than mine.
Every time I wanted to run my rai...
Delete a Clearance session after some time of inactivity
This note describes how to kick a user out of a Rails application after she hasn't requested an action for a while. Note that this is different from deleting sessions some time after the last login, which is the default.
Also note that this is probably a bad idea. Most sites keep sessions alive forever because having to sign in again and again is quite inconvenient for users and makes your conversion rates go down the toilet. [The Clearance default is to keep sessions around for one year](https://makandracards.com/makandra/701-when-sessio...
A few hints when upgrading to Ruby 1.9
Note: If you are currently working with Ruby 1.8.7 or 1.9.3, we recommend to upgrade to Ruby 2.1 first. From our experience this upgrade is much smoother than the jump from 2.1 and 2.2, while still giving your the huge performance gains of Ruby 2. Also, if you're on Ruby 1.8.7, we recommend to skip a troublesome upgrade to 1.9.3 and go straight to 2.1.
When trying to make a Rails app run on Ruby 1.9, you're likely to encounter several issues. Here are a few solutions (obviously not exhaustive):
When running `bundle ...
Resque: Clearance authentication for dashboard
Resque comes with its own dashboard (Resque server) that you can mount inside your Rails 3 application with
#config/routes.rb:
require 'resque/server'
My::Application.routes.draw do
# ...
mount Resque::Server => '/resque'
end
Unfortunately, since this bypasses the filters in your ApplicationController
, everyone can access this dashboard now (unless you have some Rack-based authentication in place, like Devise).
If you're using ...
How to: Client-side language detection
When you have a localized website, you may want to redirect users to their preferred language when they visit the root path.
Here is how to do it without a server-side component (like a Rails application).
- Use JavaScript's
navigator.language
(real browsers and IE11+) andnavigator.userLanguage
(old IEs). - Use a
<meta>
refresh as fallback - Provide buttons for paranoid users that disabled JavaScript and meta refreshs.
JavaScript
The following JavaScript will try to auto-detect a user's preferred language.
It understands string...
jpmcgrath/shortener
Shortener is a Rails Engine Gem that makes it easy to create and interpret shortened URLs on your own domain from within your Rails application. Once installed Shortener will generate, store URLS and “unshorten” shortened URLs for your applications visitors, all whilst collecting basic usage metrics.
rails/activeform
A different take on what we're doing with ActiveType. Since it lives under the rails
organization it might be part of Rails 5?
EdgeRider 0.3.0 released
EdgeRider 0.3.0 adds support for Rails 4.1 and Ruby 2.1. It forward-ports ActiveRecord::Base.scoped
to Rails 4.1.
Using Arel to Compose SQL Queries
Arel is a library that was introduced in Rails 3 for use in constructing SQL queries. Every time you pass a hash to where, it goes through Arel eventually. Rails exposes this with a public API that we can hook into when we need to build a more complex query.
gammons/fake_arel - GitHub
Gem to get Rails 3's new ActiveRecord query interface (where
, order
) and the new scope syntax (chaining scope definitions) in Rails 2.
You also get #to_sql
for scopes.
Working around the ancestry gem's way of object destruction
The ancestry gem allows you to easily use tree structures in your Rails application.
There is one somewhat unobvious pitfall to it: its way of applying the orphan_strategy
which defines how it handles children of an object going to be destroyed.
What's this all about?
In many cases you might want to disallow destruction if there are any child nodes present. The restrict
strategy does the trick but raises an exception when destroy
is called:
has_ancestry :orphan_st...
Test downstream bandwidth of Internet connection
You want to test your 1GE or 10GE internet uplink? We needed to ensure we have full 10GE to the backbone for a customer project.
Using netcat
To test whether we can achieve the bandwidth internally, you can use netcat and dd like this:
On your first server: nc -v -l 55333 > /dev/null
On your second server: dd if=/dev/zero bs=1024K count=5000 | nc -v $remote_ip 55333
You should see some output like this:
user@xxx:~ % dd if=/dev/zero bs=1024K count=5000 | nc -v removed 55333
Connection to 91.250.95.249 55333 port [...
Limiting CPU and memory resources of Paperclip convert jobs
If you're using Paperclip to store and convert images attached to your models, processing a lot of images will probably cause headache for your system operation colleagues because CPU and/or memory peaking.
If you're on Unix you can use nice
to tell the Kernel scheduler to prefer other processes that request CPU cycles. Keep in mind that this will not help if you're running into memory or IO trouble because you saved some bucks when you ordered (slow) harddrives.
ImageMagick (the tool which is used by Paperclip to do all that funky ima...
Mysql::Error: SAVEPOINT active_record_1 does not exist: ROLLBACK TO SAVEPOINT active_record_1 (ActiveRecord::StatementInvalid)
Possible Reason 1: parallel_tests - running more processes than features
If you run old versions of parallel_tests with more processes than you have Cucumber features, you will get errors like this in unexpected places:
This is a bug caused by multiple processes running the same features on the same database.
The bug is fixed in versions 0.6.18+.
Possib...
Updated: Unpoly + Nested attributes in Rails: A short overview of different approaches
With the new unpoly client side templates (available since 3.10) there's another way to substitute the ids of inserted nested attribute forms which I added to this card.
Fixing "A copy of Klass has been removed from the module tree but is still active"
This can happen during development when classes without automatic reloading are pointing to classes with automatic reloading. E.g. some class in lib
is calling Model.static_method
.
Workaround A
Stop referencing autoloaded classes from static files. If you can't, see workaround B and C.
Workaround B
Make sure the offending file (the one referencing the autoloaded class) is autoloaded, too. You may do this:
# config/application.rb
config.paths.add 'offending/file/parent/directory', eager_load: true
Workaroun...
Workaround for broken integer division after requiring the mathn library
Ruby's mathn library changes Fixnum
division to work with exact Rational
s, so
2 / 3 => 0
2 / 3 * 3 => 0
require 'mathn'
2 / 3 => Rational(2,3)
2 / 3 * 3 => 2
While this might sometimes be quite neat, it's a nightmare if this gets required by some gem that suddenly redefines integer division across your whole project. Known culprits are the otherwise excellent distribution and [GetText](https://g...
ActiveRecord: How to use ActiveRecord standalone within a Ruby script
Re-creating a complex ActiveRecord scenario quickly without setting up a full-blown Rails app can come in handy e.g. when trying to replicate a presumed bug in ActiveRecord with a small script.
# Based on http://www.jonathanleighton.com/articles/2011/awesome-active-record-bug-reports/
# Run this script with `$ ruby my_script.rb`
require 'sqlite3'
require 'active_record'
# Use `binding.pry` anywhere in this script for easy debugging
require 'pry'
# Connect to an in-memory sqlite3 database
ActiveRecord::Base.establish_connection(
ad...
Saving application objects in your session will come back to haunt you
If you save a non-standard object (not a String or Fixnum, etc) like the AwesomeClass
from your application in the session of visitors be prepared that some time you will get this exception:
ActionController::SessionRestoreError: Session contains objects whose class definition isn't available. Remember to require the classes for all objects kept in the session. (Original exception: ...)
This happens when you remove your AwesomeClass
but users come back to your site and still have the serialization of such objects in their session....
Add a prefix to form field IDs
If you use a form (or form fields) multiple times inside one view, Rails will generate the same id
attributes for fields again.
This card presents you with a way to call something like
- form_for @user, :prefix => 'overlay' do |form|
= form.text_field :email
and get this as HTML:
<input name="user[email]" id="overlay_user_email" (...) />
You can also put a :prefix
into a field's options. Note how only the id
but not the name
changes as we would not want to pass an overlay_user[email]
param to the controller. Sett...
How to drop all tables in PostgreSQL
To remove all tables from a database (but keep the database itself), you have two options.
Option 1: Drop the entire schema
You will need to re-create the schema and its permissions. This is usually good enough for development machines only.
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO public;
Applications usually use the "public" schema. You may encounter other schema names when working with a (legacy) application's database.
Note that f...
Virtual attributes for date fields
Note that this card is very old. You might want to use ActiveType for your auto-coerced virtual attributes instead.
We sometimes give our models virtual attributes for values that don't need to be stored permanently.
When such a virtual attribute should contain Date
values you might get unexpected behavior with forms, because every param is a string and you don't get the magic type casting that ...