Search_spinner Cross_grey Magnifier
help

Search syntax

  • Search for repeating cards with repeats:yes
  • Search for unfinished cards with unfinished:yes
  • Search for cards in a certain topic with topic:"name"
  • Search for phrases with "a phrase"
  • Exclude a word from search: -word
  • Exclude a phrase from search: -"a phrase"
  • Exclude a topic from search: -topic:"name"

PostgreSQL vs MySQL: How to update using a join

When performing an UPDATE statement, you may want to join a table to update from. It works differently in MySQL and PostgreSQL.

MySQL

UPDATE profiles
INNER JOIN users ON profiles.user_id = users.id
SET public = users.visible

PostgreSQL

UPDATE profiles
SET public = users.visible
FROM users
WHERE profiles.user_id = users.id

RubyMine: Scratch files

There are times when you have a chunk of text that you want to do something with, e.g. replace something on it, or quickly edit it.

While you can open your favorite non-RubyMine editor for this, there is also a plugin: Scratch.

It allows RubyMine to open temporary files (actually they are saved, but somewhere inside the plugin’s directory) so you don’t need to switch to a text editor like gEdit that works differently and may not even offer what you are used to.

Install it like this:

  1. Open…

Make timestamp of dmesg in Ubuntu human readable

dmesg shows the kernel ring buffer containing low-level system messages.
Per default, dmesg shows a timestamp:

12:59:26 fnordomator ~ > dmesg | tail
[101925.211846] usb 2-1.1: USB disconnect, device number 16
[110486.855788] usb 2-1.1: new high-speed USB device number 17 using ehci_hcd

If you’re a human, use dmesg -T to print the timestamp human readable:

12:59:31 fnordomator ~ > dmesg -T | tail
[Di Apr 21 12:43:16 2015] usb 2-1.1: USB disconnect, device number 16
[Di Apr 21 15:05:57 2015] usb 2-1.1: new hig...
Repeats

Faking and testing the network with WebMock

WebMock is an alternative to FakeWeb when testing code that uses the network. You should probably learn it together with RestClient, which is an awesome alternative to net/http and shares many concepts with WebMock.

You can fake a remote server response like this:

stub_request(:get, 'http://host/api').to_return(:body => 'fake body')

You can test that a request has happened like this:

stub_request(:get, '...

Make "rake notes" learn about Haml, Sass, CoffeeScript, and other file types

Rails comes with a Rake task notes that shows code comments that start with “TODO”, “FIXME”, or “OPTIMIZE”.

While it’s generally not good practice to leave them in your code (your work is not done until it’s done), in larger projects you will occasionally have to use them as other parts of the application that you depend upon are not yet available.
To keep track of them, run rake notes. Its output looks something like this:

$ rake notes
app/controllers/fron...

Rubymine: Code folding

Code folding is a very useful feature to me. It gives me a quick overview over a file and keeps me from scolling like a hamster in its wheel.

Keyboard shortcuts:

Collapse/expand current code block
strg -/+
Collapse/expand the whole file
strg ctrl -/+

When diving into Cucumber features or huge Ruby classes, I usually collapse all and the gradually expand what I need.

PSA: "index: true" in Rails migrations does not work as you'd expect

Several Rails migration methods accept index: true as an option to create an index. In some cases, this silently does not work. Know exactly what you are doing, or do not use it and use add_index instead.

Example

Consider the following migration.

class CreateExamples < ActiveRecord::Migration
  def change
    create_table :examples do |t|
      t.references :category, index: true
      t.boolean :positive, index: true
      t.integer :number_of_participants, index: true
    end

    add_reference...

[Bugreport] Symbolized Strings Break Keyword Arguments in Ruby 2.2

TL;DR Under certain circumstances, dynamically defined symbols may break keyword arguments in Ruby 2.2. This is fixed in Ruby 2.3-dev.

Specifically, when
- there is a method with several keyword arguments and a double-splat argument (e.g. def m(foo: 'bar, option: 'will be lost', **further_options))
- there is a dynamically created Symbol (e.g. 'culprit'.to_sym) that is created before the method is parsed
- the method gets called with both the option and a culprit keyword argument

then the option keyw…

Using mime types with send_file

When using send_file (for example for attachments of any kind), make sure your application knows the correct mime types so that all browsers can handle the files. It is much more convenient for users if they can decide to open a file directly instead of having to save it first.

For Rails >= 3.2

Simply put your mime types in config/initializers/mime_types.rb. send_file will take care of everything else.

For Rails < 3.2

Put your mime types in config/initializers/mime_types.rb. Additionally, tell send_file to use them (for ex…

External contentRepeats

Ruby facets

Ruby Facets is the premiere collection of general purpose method extensions and standard additions for the Ruby programming language.

RSpec: Tagging examples and example groups

In RSpec you can tag examples or example groups with any tags you like simply by saying

describe ReportCreator, slow: true do
  # ..
end

describe ReportCreator do
  it 'generates reports', slow: true do
    # ...
  end
end

You can then only run examples with these tags.

rspec --tag slow
rspec -t slow

# Using the parallel_tests gem
rake "parallel:spec[,,--tag slow]"

Or you can run all examples except the ones with a certain tag:

```
rspec –tag ~slow # note the ~
rspec -t ~slow

Using the parallel_tests gem

rake “par…

ActiveRecord: Automatically remove join records on has_many :through associations

# Given the following models

class Image < ActiveRecord::Base
  has_many :album_image
  has_many :albums, through: :album_images
end

class Album < ActiveRecord::Base
  has_many :album_image
  has_many :images, through: :album_images
end

class AlbumImage < ActiveRecord::Base
  # Join model
  belongs_to :album
  belongs_to :image
end

Destroying a record in this setup will only remove the record itself, and leave orphaned join records behind.

```
image = Image.last
image.destroy # removes only the image record,
# but…

A solid and unobtrusive plugin for form field placeholders

jquery-placeholder is a simple jQuery plugin that enables form placeholders in browsers that do not support them natively, i.e. IE < 10.

Properties

  • Works in IE6.
  • Automatically checks whether the browser natively supports the HTML5 placeholder attribute for input and textarea elements. If this is the case, the plugin won’t do anything. If @placeholder is only supported for input elements, the plugin will leave those alone and apply to textareas exclusively. (This is the case for Safari 4, Opera 11.00, and possibly other browsers.) …
External content

What Ruby’s ||= (Double Pipe / Or Equals) Really Does

||= and &&=

a ||= b
a || a = b

a &&= b
a && a = b

[op]=

a += b
a = a + b

a -= b
a = a - b

# Other operators, e.g. Array#| (union)
ary1 = ['a', 'b', 'c']
ary2 = ['c', 'd', 'a']

ary1 |= ary2
ary1 = ary1 | ary2
# => ['a', 'b', 'c', 'd']
Repeats

faviconit.com: Most comfortable and free favicon generator

Eduardo Russo was tired of complex favicon creation and created his own favicon generator. It’s really easy and allows a lot of image editing before rendering the favicons, in all needed sizes, formats and with the HTML needed to include them!

Unfreeze a frozen ActiveRecord

You can freeze any Ruby object to prevent further modification.

If you freeze an ActiveRecord and try to set an attribute you will an error like this:

can't modify frozen hash

This is because ActiveRecord delegates #freeze to its attributes hash.

You can unfreeze most Ruby objects by creating a shallow copy of the frozen object by calling #dup on it:

user = User.find(3)
user.freeze
unfrozen_user = user.dup

Notes for Rails 2 users —————–…

Ruby: How to grow or shrink an array to a given size

If you want to grow a Ruby Array, you might find out about #fill but it is not really what you are looking for. [1]
For arrays of unknown size that you want to grow or shrink to a fixed size, you need to define something yourself. Like the following.

Array.class_eval do

  def in_size(expected_size, fill_with = nil)
    sized = self[0, expected_size]
    sized << fill_with while sized.size < expected_size
    sized
  end

end

Use it like this:

>> [1, 2, 3].in_size(5)
=> [1, 2, 3, nil, nil]    ...

Proposal: A step-overriding convention for Cucumber

Cucumber steps cannot be overwritten or inherited because of their pattern-matching approach.

Sometimes you need to adapt a step for a certain use case. To make it clear that this is a variation of a step, I suggest appending [<tags>] to the step definition. This prevents ambiguous step definitions and makes it clear that this is an altered step, immediately telling the developer what the variation is about.

Example

Spreewald:

Then the "Terms of Service" checkbox should be checked

Custom step that will work with Selenium ev…

2785 cards