Popular mistakes when using nested forms
Here are some popular mistakes when using nested forms:
- You are using
fields_for
instead ofform.fields_for
. - You forgot to use
accepts_nested_attributes
in the containing model. Rails won't complain, but nothing will work. In particular,nested_form.object
will benil
. - The
:reject_if
option lambda in youraccepts_nested_attributes
call is defined incorrectly. Raise the attributes hash given to your:reject_if
lambda to see if it looks like you expect. - If you are nesting forms into nested forms, each model involved ne...
Check whether an element is visible or hidden with Javascript
jQuery
You can say:
$(element).is(':visible')
and
$(element).is(':hidden')
jQuery considers an element to be visible if it consumes space in the document. For most purposes, this is exactly what you want.
Native DOM API
Emulate jQuery's implementation :
element.offsetWidth > 0 && element.offsetHeight > 0;
jQuery > 3
Query 3 slightly modifies the meaning of :visible (and therefore of :hidden).
Emulate jQuery'...
Be careful when closing expanded stories in Pivotal Tracker
When you open up a story to only have a look at it, close it by pressing the "Cancel" button (or Esc key).
Clicking the arrow in the top left is the same as pressing the "Save" button. If someone changes the story description in the meantime you will overwrite their changes with everything you see, even if you did not change anything. If you only want to take a peek, prefer to hover the speech bubble icon.
If your story gets shotgunned and you notice it, you are lucky -- and you may be able to get your changes back. Open up the his...
How to build the perfect number of blank records for a nested form
When you render a nested form for a Movie
which has_many :actors
, you want to render the right number of blank Actor
forms. The right number means:
- A minimum number of blank forms so the user can add more
Actors
to an existingMovie
, e.g. 2. - A minimum total number of forms (both blank and pre-filled) so the user sees more than just 2 blank
Actor
forms when she is enteringActors
for the first time, e.g. 5.
For the example above, this is the desired progression of the number of blank forms:
| Number of actors | Number of ...
mysqltuner.pl
This Perl script will run diagnostics on your MySQL database and recommend changes to your MySQL configuration.
How to upgrade RubyMine
This card explains how to upgrade an existing RubyMine installation to a newer version. If you're installing RubyMine for the first time, see install RubyMine under Ubuntu. You might also consider installing RubyMine with snap, so it can receive automatic updates (also described in the install card).
This procedure ensures that an update does not totally break your IDE, as it allows you to keep both the previous and the new version of RubyMine:
- [Download the newest version](http://www.jetbrains.com/ruby...
Order in which RSpec processes .rb files
Because your examples should not change global state, you should not need to care about the order in which RSpec processes your .rb
files. However, in some cases you might want to know.
RSpec 3
- Runs
.rb
files in alphabetical order of their file paths by default (or when you specify--order defined
). - You run t...
New Cucumber Factory makes it easier to associate records
I pushed a new version of the Cucumber Factory gem. This new release lets you refer to a previously created record by any string attribute:
Given there is a movie with the title "Before Sunrise"
And there is a movie with the title "Limitless"
And there is a movie with the prequel "Before Sunrise"
Note how we didn't have to explicitly give the prequel a name in the example above. This is still possible, but will rarely be necessary now:
Given "Before Sunrise" is a movie with...
Dynamically skip Capistrano hooks
When you have a hook in your Capistrano file that dumps your remote database, you might not want it to dump each time you deploy (say, you're experimenting with staging
and don't want ten dumps an hour). How to skip dump creation:
Capistrano 2
In your Capistrano file:
before 'deploy:update_code', 'db:dump' unless fetch(:skip_dump, false)
The second parameter of fetch
sets a default value if skip_dump
is not defined.
In your terminal:
cap staging deploy -S skip_dump=true
The -S
must be a capital letter t...
Merging two arbitrary ActiveRecord scopes
(Rails has a method ActiveRecord::Relation#merge
that can merge ActiveRecord scopes. However, its behavior has never been clear, and in Rails 7 it still discards conditions on the same column by the last condition. We discourage using #merge
!)
The best way to merge ActiveRecord scopes is using a subquery:
scope_a.where(id: scope_b)
It is a little less concise than #merge
, but unambiguous.
Example
Assume a model where a deal has many documents:
class Deal < ApplicationRecord
has_many :...
SSL: Build a Certificate signing request (CSR)
In order to request a SSL certificate from any dealer, you usually need a CSR certificate. As both the CSR as well as key are created in this step, make sure you save this certificate on a trusted, secure machine only. Usually this is your production environment.
Run this on the server (not on your machine) as root.\
Replace your-domain.tld with the domain you request the certificate for and YYYY with the current year so you will not have any conflicts when requesting a certificate next year.
openssl req -new -sha256 -out www.your-dom...
Semantic markup standard for search engines
If you would like to enrich your website with semantic markup like contact data, places or events you should have a look at schema.org. "Search engines including Bing, Google and Yahoo! rely on this markup to improve the display of search results, making it easier for people to find the right web pages."
The following example from the schema.org documentation shows you how to describe a movie with semantic markup:
<div itemscope itemtype ="http://schema.org/Movie">
<h1 itemp...
Enable soft tabs in Vim
If you want to enforce soft tabs (spaces instead of tabstops) in Vim put this into your ~/.vimrc
(Linux) or C:\Users\<Username>\_vimrc
(Windows):
set tabstop=2
set softtabstop=2
set shiftwidth=2
set expandtab
You can also call those commands in a running Vim instance (using ":set ...
") to switch on soft tabs temporarily.
On a side note: remember the :mkvimrc
command that saves active settings to a vimrc file.
Find records created within a timespan
Put the attached file into config/initializers/
to be able to say created_within
on any ActiveRecord
or its scope chain. This will return a scope of all records created between two given timestamps.
Use it like that:
Booking.created_within(1.year.ago, Date.yesterday).count
User.active.created_within(1.hour.ago, DateTime.now)
It explicitly looks at the database table name so you are usually fine when joining other tables.
Defining host aliases in your SSH config
You probably already manage servers you often connect to inside the ~/.ssh/config
file. What is nice: you may define alias names for hosts so that you can say something like ssh foobar-staging
.
This is especially helpful for servers whose hostnames are hard to remember or who don't have a DNS record at all (and must be accessed via their IP address).
To achieve the above, you can define something like this in your ~/.ssh/config
:
Host foobar-staging
Hostname staging.example.com
Note that SSH will only match this for `ssh f...
How to employ and run your tests with parallel_tests to speed up test execution
When your cucumber features grow massively over time, the test execution can take a lot of time.
One easy way to speed up your test execution is to use the parallel_tests gem.
It comes along with some useful rake tasks that let you setup your local test environment shortly to run your features, specs or unit-tests in parallel.
Follow these steps to get it to work.
-
Add the parallel_tests gem to your
Gemfile
test sections like that:# ./Gemfile group :development, :test do
...
Script to create and copy a production dump to your project root
Soon after having written our shell-for script, we wanted to easily get dumps of our productions machines, too. This is how we do it:
dump-for staging [-s]
It will copy the dump to your project's tmp
directory and name it according to the capistrano stage you're calling for, here: staging.dump
. When you pass the optional -s
option, the dump will automatically been sourced into your local development database.
This script ...
Releasing geordi
After having written useful scripts into makandra notes for a long time, we’ve now tied them into a powerful new gem: geordi.
It gives you the power to
- get a dump from your production database with
dump-for production
- install your local gems to the production machine with
install-gems-remotely
- execute rake tasks to several environments at the same time with
power-rake db:migrate
- and much more
Rails 3.1: Release candidate
Asset pipeline, HTTP streaming, jQuery as default framework, auto-reversable migrations, identity map for ActiveRecord.
Ruby 1.8.x support will be dropped with or after Rails 4.
Fix randomly failing PDF cucumber tests
Sometimes PDF cucumber tests fail at the first test run and succeed at the second run. You can fix this with this step:
When PDF generation is ready
And I follow "Save as PDF"
# Checks for PDF contents go here
Here is the step definition:
When /^PDF generation is ready$/ do
Then 'I should see ""'
end
Setting up Ubuntu Server as Virtual Machine in VMware
Install Ubuntu Server
- Download an image from this site
- Select
File > New…
to and follow the instructions, choose the.iso
file you downloaded as image file - Deselect "Simple installation" – you want to configure your system yourself
- Start the new virtual machine and follow the instructions
Install VMware Tools
- Choose
Virtual Machine > Install VMware Tools
from the VMware menu, then:
^
# install required packages
sudo apt-get install build-essential psmisc
# m...
Validations on associated objects
Validations that need to access an associated object may lead to some trouble. Let's exemplify that using this example:
class Project < ActiveRecord::Base
has_one :note
end
class Note < ActiveRecord::Base
belongs_to :project
end
Now say we need a validation that ensures that a description is set (validates_presence_of :description) within Note
but only if the Project
has a flag called external
set to true. The following code will lead to some problems as the associated object is not present when creatin...
Use find_in_batches or find_each to deal with many records efficiently
Occasionally you need to do something directly on the server -- like having all records recalculate something that cannot be done in a migration because it takes a long time.
Let's say you do something like this:
Project.all.each(&:recalculate_statistics!)
Even though you may have been successful with this on your development machine or the staging server, keep in mind that production machines often hold a lot more records. Using all
may just work, even with lots of records, but when you iterate over such records and fetch a...
Speed up Capistrano deployments using a remote cached copy of repository
You can seriously speed up deployments with Capistrano when using a local git repository on the server you are deploying to.
Simply add
set :deploy_via, :remote_cache
set :copy_exclude, [ '.git' ]
to your config/deploy.rb
and Capistrano will create a clone in shared/cached-copy
. This will be updated using git pull
when deploying which transfers less bytes and is usually much faster. If deploy_via
is set to use default settings (being "export
"), Capistrano will do a full clone of the repository from your git host otherwi...