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"

Using :dependent => :destroy – Issues with 'code arrangement' or 'cached objects'

First keep in mind that :dependent => :destroy hooks into before_destroy.
So when you use other before_destroy callbacks the sequential arrangement of your code may be important.

For example:

class Container < ActiveRecord::Base
  before_destroy :a_callback
  has_many :items, :dependent => :destroy
end

results in

container.destroy
# => a_callback
# => container.items.destroy_all

but

class Container < ActiveRecord::Base
  has_many :items, :dependent => :destroy
  before_...

How to install a debian/ubuntu package without dependencies

Please note that you can break your system with this! This is not recommended.

Sometimes the package repository has errors. If you can’t install a package because you can’t install the dependencies with the package manager you can try this:

(You have to download the deb first e.g. from here or here)

dpkg -x bad-package.deb common
dpkg --control bad-package.deb

Then you have to edit DEBIAN/control with an editor. You can delete the broken dependencies t…

Javascript equivalent of Ruby's array.collect(&:method)

The most common use case for Ruby’s #collect is to call a method on each list element and collect the return values in a new array:

['hello', 'world', 'this', 'is', 'nice'].collect(&:length)
# => [5, 5, 4, 2, 4]

Although there is no equivalent to this idiom in naked Javascript, there is a way to collect object properties (but not method results) if you are using common Javascript libraries.

If you are using jQuery with the Underscore.js utility library, you can use [pluck](htt…

You can use any RSpec matcher to match arguments of method calls

RSpec lets you define the arguments with which you expect a method to be invoked:

subject.should_receive(:say).with('hello')

Sometimes you don’t care about the exact arguments. For such cases RSpec comes with argument constraints like anything or hash_including:

subject.should_receive(:update_attributes).with(hash_including(:message => 'hi world'))

You can go even further and use any RSpec matcher to match arguments:

subject.s...

Mute a skype chat

If you want to stop getting notified about new messages in a Skype chat (but not leave it altogether), you can type

/alertsoff

into the chat. This will only affect this one chat.

To only be notified when certain keywords appear in the conversation, use

/alertson KEYWORD

Note that multiple keywords must be defined with one call, as each call will overwrite previous keywords. Separate them by spaces:

/alertson KEYWORD1 KEYWORD2

Keywords are case-insensitive.

To revert to standard behaviour, use simply

/alertson    ...

How to change the order of nested forms being rendered (especially blank forms)

Generally for nested forms, a blank form is placed below all other existing object forms. If you would like to change the position of the blank form(s) you can reorder the object’s one-to-many association. For example you can put the blank form on top with the following snippet:

actors = movie.actors
actors.build_for_form(1, 1)
actors.unshift(actors.pop(1))

Because build_for_form creates new objects and appends them to the one…

Fix multiple CKEditor instances using jQuery adapter - fixed since 4.2

Using the jQuery adapter breaks the built-in save function of CKEditor.

Phenomenon: The page is submitted correctly, but the original values of the form fields were posted instead of what was typed in the editors.

Work around: Basicly instead of initiating the editor using the above example I ended up using the following:

$( 'textarea.editor').each( function() {

    CKEDITOR.replace( $(this).attr('id') );

});

Note: This assumes that each field using the editor has its own unique ID.

Paste X selections into your terminal with your keyboard

The buffer where text is put into when you double-click on text within X is called X selection.

You can paste this buffer (at least into a terminal window) by pressing Shift + Insert.

Rails logs are not flushed automatically (in Rake tasks)

The Rails logger will store its content in a buffer and write it into the file system every 1000 lines. This will come back to bite you when using Rails.logger.info to write log output during Rake tasks or on a production console.

You often won’t notice this because for the development and test environments auto_flushing is set to write after each line. On production environments the Rails logger writes only every 1000 lines – and not upon shell or script ter…

Don't call gsub on safe strings

Calling #gsub on a string that was previously marked as #html_safe will lead to unexpected behavior. E. g. backreferences to captured groups ($1, $2) will be nil even if the group was matched.

There is no universal workaround available since you can’t expect #html_safe strings to still be safe after using gsub on them.

You can, however, fix the $1 gsub behavior on html_safe strings.

Mailcatcher: An alternative to inaction_mailer

Looks simpler than inaction_mailer:

sudo gem install mailcatcher
mailcatcher

Setup Rails to send mails to 127.0.0.1:1025. Usually you want the following config in config/environments/development.rb and maybe in test.rb or cucumber.rb.

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address => 'localhost',
  :port => 1025
}

Now you can see sent mails in your browser when opening http://127.0.0.1:1080

Note: In order t…

Defining to_json and avoiding errors

Defining a custom to_json method for your classes can be useful. Do it properly or you will “randomly” get errors like these:

wrong number of arguments (1 for 0) (ArgumentError)
wrong number of arguments (0 for 1) (ArgumentError)

Take a look at how Object#to_json is defined:

def to_json(options = nil)
  ActiveSupport::JSON.encode(as_json(options))
end

Make sure you at least take the options argument – or, if you don’t need to look at it, just grab and (if you need to) pass on any arguments you receive like this…

Parse & sort unique hits in logfiles

If you want to know the exact hits on your website (or whatever logfile you want) for a specific date without duplicates, here’s how. “Unique” means you don’t want to count hits to an URL originating from the same IP twice.

You can use the attached script to do so:

# ./log_parser.rb 2011-10-04

27 hits on /rss.xml
36 hits on /stylesheets/fonts/slkscr-webfont.woff
37 hits on /stylesheets/fonts/slkscrb-webfont.woff
37 hits on /images/bullet.png
38 hits on /images/download.png
38 hits on /images/play.png
39...

Monitor a Rake task with God

In order to monitor a Rake task using God your Rake file must write a file with its process ID (PID) to a path determined by God. This way God can check whether the Rake process is still alive.

Here is how to do this: In your God config, call the Rake task with an environment variable PIDFILE. This variable should equal the PID file path desired by God:

God.watch do |w|
  w.dir = "#{rails_root}"
  w.name = "my_task"
  w.interval = 10.seconds
  w.pid_file = "#{rails_root}/tmp/pids/#{w.name}...

Capybara can match elements outside of <body>

Capybara will match elements outside of a page’s <body> tag.

For example, the step definitions below match <link> nodes in a page’s <head>:

Then /^my browser should auto-discover the "([^"]*)" feed$/ do |slug|
  page.should have_css(
    'head link' +
    '[rel="alternate"]' +
    "[href='http://www.example.com/#{slug}/feed.rss']" +
    '[title="RSS feed (all cards)"]' +
    '[type="application/rss+xml"]'
  )
end

Then /^my browser should not auto-discover any RSS feeds$/ do
  page.shou...

How to update a MySQL column with ascending numbers

Given the problem you have a new column postion and that column should be updated for all existing rows with ascending numbers. Furthermore these numbers should be generated by a special order. In order to achieve that you could do the following:

execute "SET @pos := 0;"
update " UPDATE pages SET position = ( SELECT @pos := @pos + 1 ) ORDER BY updated_at DESC;"

Disable Flash player in Google Chrome

Typing about:plugins in the URL bar will give you a menu where can disable and enable individual plugins.

2786 cards