Find geocoded records that are close to a location (radius search)
When you have objects in your database that hold latitude and longitude and you want to find others that are close to given coordinates you can use the Graticule gem.
Graticule
Graticule offers several methods to compute the distance between two geo-dated objects but fetching records from the database that are within a given radius of a location is a bit trickier:
def close_destinations(latitude, longitude)
distance_sql = Graticule::Distance::Spherical.to_sql(:latitude => l...
"Require group" doesn't work with Ubuntu's default Apache installation
If you want to use Require group $GROUPNAME
on your default Apache installation like this:
<Directory "/var/www/foobar">
Order allow,deny
Allow from all
Options None
AllowOverride all
AuthName "Area 51"
AuthType Basic
AuthBasicProvider external
AuthExternal pwauth
Require group admin
</Directory>
You need to first install libapache2-mod-auth-sys-group
. You don't get any error message if you haven't installed it but it doesn't work.
sudo apt-get install...
Declare different CSS background-images for different locales
If you would like to use language specific layout (e.g. background-images) in your applications stylesheets you can achieve this easily by using the lang
attribute in your views (ERB):
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= I18n.locale || 'en' %>" lang="<%= I18n.locale || 'en'%>">
...
</html>
or in HAML:
%html :xmlns => "http://www.w3.org/1999/xhtml", :"xml:lang" => I18n.locale || 'en', :lang => I18n.locale || 'en'
Then, in your stylesheet you can for example declare different background-images fo...
How to manage Thunderbird mailing lists
You can group contacts using a mailing list in Thunderbird for easier access and less trouble when trying to find out who to send a message to. Here is how to do this:
- Open your Thunderbird address book
- Press the "New List" button
- Choose a name for your list and add contacts. There will be auto completion from your address book.
When sending e-mails you can now add a recipient with the mailing list's name and the e-mail will be sent to all its members.
Preparing your test database: mind the differences
Instead of running all missing migrations on your test database with rake db:migrate RAILS_ENV=test
you can also use a handful of rake tasks to prepare the database structure directly. They can produce different results, though.
In a nutshell, to ensure your test database gains the correct structure:
- Don't use
rake db:test:prepare
carelessly - or use
rake db:test:clone_structure
← preferred :) - or use
rake db:migrate RAILS_ENV=test
and don't mix it with other ways, like some of the rake tasks.
rake db:test:prepare
------------...
state_machine: Test whether an object can take a transition
When using state_machine you sometimes need to know whether an object may execute a certain transition. Let's take an arbitrary object such as a blog article as an example that has those states:
A -> B -> C -> D
Additionally, you have transitions between the states as shown above. Let's call the transition between 'A' and 'B' transition_ab
for simplicity. \
Given an object in state 'A'. You can call object.transition_ab
but calling object.transition_bc!
will obviously fail because ...
Login forms: Disable browser prompt to remember the password
In order to prevent the browser from asking whether to remember the password, give a form an autocomplete
attribute with the value off
:
<form "/session" method="post" autocomplete="off">
...
</form>
Rails example
form_for @model, :html => { :autocomplete => "off" } do |form|
Ruby: Downloading files from the Internet
This is easy:
require 'open-uri'
File.open('/target/path/to/downloaded.file', "wb") do |file|
file.write open('http://example.com/your.file').read
end
Basic Authentication
When your file is protected by HTTP Basic Authentication
, pass your credentials as hash:
File.open('/target/path/to/downloaded.file', "wb") do |file|
file.write open('http://example.com/your.file', :http_basic_authentication => [your_username, your_password]).read
end
Rails 3.1 error message: Could not find a JavaScript runtime
After starting the Rails server in a freshly generated Rails 3.1 project you could see an error message such as
/usr/lib/ruby/gems/1.8/gems/execjs-1.3.0/lib/execjs/runtimes.rb:50:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
Just add a JavaScript runtime to your Gemfile and the error vanishes.
Examples:
gem 'therubyracer'
gem 'extjs'
MySQL: Do not use "WHERE id IN (SELECT ....)"
Note: This applies specifically to MySQL. In PostgreSQL for example, this is not an issue.
If you care about performance, never use a query like
UPDATE users SET has_message = 1 WHERE users.id IN (SELECT user_id FROM messages)
MySQL does not optimize this and seems to scan the temporary table, which isn't indexed, for every row in the update statement. This applies to other statements than UPDATE
as well.
Instead, either use a JOIN
like
UPDATE users INNER JOIN messages ON messages.user_id = users.id SET has_message =...
Change the MySQL default character set on Amazon Relational Database Service (RDS)
Look here for informations how you can show the MySQL default character set.
At first you need the Amazon RDS Command Line Toolkit
- download and unzip the [Amazon RDS Command Line Toolkit](http://aws.amazon.com/developertools/A...
How to use html_safe correctly
By default, Rails views escape HTML in any strings you insert. If you want to insert HTML verbatim, you need to call #html_safe
. However, #html_safe
does not "unescape" a string. It merely marks a string as safe for unescaped insertion.
How html_safe works
Calling html_safe
on a String
returns a new object that looks and acts like a String
, but actually is a ActiveSupport::SafeBuffer
:
"foo".length
# => 3
"foo".class
# => String
"foo".html_safe.length
# => 3
"foo".html_safe.class
# => ActiveSupport::S...
Cucumber.yml was found, but could not be parsed.
If you encounter the error message above when running cucumber, just execute...
rm rerun.txt
...in the Rails directory.
Or run...
tests
...from the geordi gem. This will do the work for you automatically.
Stub methods on any instance of a class in Rspec 1 and Rspec 2
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 this feature built in:
User.any_instance.stub(:foo => :bar)
user = User.new
user.foo
# => :bar
RSpec 3
-------...
Show the character set and the collation of your MySQL tables
To show the collation of your tables you have to login to the MySQL console and execute SHOW TABLE STATUS FROM database;
mysql> SHOW TABLE STATUS FROM test;
+-------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_leng...
Show and change MySQL default character set
To show the MySQL default character set you have to login to the MySQL console and execute SHOW VARIABLES LIKE 'char%';
mysql> SHOW VARIABLES LIKE 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| chara...
Creating a patch in git and how to apply patches
You can convert git commits into patch files. Those can be used to apply to a different repository [1] or by someone else (e.g. sent when sent to them via e-mail).
Creating a patch in git
- Make your changes and commit them.
- Run
git format-patch COMMIT_REFERENCE
to convert all commits since the referenced commit (not including it) into patch files.
For example, let's say you prepared 2 commits. Run:
git format-patch HEAD~~
This will create 2 files, one for each commit since HEAD~~
, like these:
00...
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_...
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.
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...
Why your Cucumber feature loses cookies when run under Selenium
When your Cucumber feature seems to forget cookies / sessions when you run it with Selenium check if the test travels in time like here:
Given the date is 2017-10-20
When I sign in
Then I should see "Welcome!"
What happens here is that the Rails application serving pages runs in 2017, but the process running your browser still lives today. This huge gap in time will expire most cookies immediately.
If all you need is to freeze the time to a date, a workaround is to travel to the future instead.
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 R...
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.
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"]',
visible: false
)
end
Then /^my browser should not auto-discover any RSS fe...