ActsAsTaggableOn: Cache tag lists
For performance improvements (and to remove the need for eager loading), the ActsAsTaggableOn gem supports caching your tag lists directly in your model. To enable this, simply add a cached_tag_list column to your table.
Example:
class Company < ActiveRecord::Base
acts_as_taggable_on :categories
end
The cache column has to be named cached_category_list.
Existing data
If you already have existing data, you have to save all records with tags once, after you've added the ...
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...
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:preparecarelessly - or use
rake db:test:clone_structure← preferred :) - or use
rake db:migrate RAILS_ENV=testand 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 ...
Large forms are slow on the iPad
- Forms with many inputs (600+ in my case) become extremely unresponsive on an iPad, up to the point where it can take several seconds for a control to respond to touch commands.
- This is true for both iPad 1 and iPad 2 models.
- While certain CSS styles can lead to performance issues, removing those styles won't help if the form simply is very large.
- A workaround is to only show a limited number of form inputs at the time, e. g. by toggling groups of form...
jQuery 1.7 Released
Two new methods on and off are the new way of declaring event handlers. bind, delegate and live area deprecated. Also better performance for delegated events.
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 =...
How the Clearance gem remembers and clears sessions
Clearance is a gem that provides authentication functionality (e.g. login, logout). This note explains, how the clearance login, logout and (in old Clearances) remember me functionality works.
Login
Clearance defines a database column called "remember_token". When you login in, that token will be saved in a cookie. For that reason you don't have to re-sign-in when you close and open the browser again.
This also means that you can be logged in in more than a single browser. Also see [When ses...
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...
Improve web font rendering in Windows by autohinting fonts
Web fonts are awesome. After being restricted to Arial for two decades there is finally a cross-browser way to embed fonts into web pages.
Unfortunately while web fonts look awesome on Linux and MacOS, they look horrible on Windows, a problem that gets worse with smaller font sizes.
The culprit is something called font hinting:
...
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...
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_REFERENCEto 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...
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;"
Properly adding fields with default values to a model
When adding a new field to your model's database table, don't set any defaults in the database.
It makes people wonder why they get such values when reading attributes.\
Why? Because nobody looks at the database layout since such things are part of your application's logic -- and thus they belong into the corresponding model.
How to
Do it like this:
-
In your migration, after adding the field, update all fields to your desired default:
update "UPDATE users SET locked = #{quoted_false};" -
In your model, set a defau...
Single step and slow motion for cucumber scenarios using @javascript selenium
Single step and slow motion for Cucumber scenarios can come in handy, especially in @javascript scenarios.
# features/support/examiners.rb
AfterStep('@slow_motion') do
sleep 2
end
AfterStep('@single_step') do
print "Single Stepping. Hit enter to continue"
STDIN.getc
end
If you're using spreewald, these tags are available as @slow-motion and @single-step (with dashes instead of underscores).
Note: You can also [prevent the selenium webbrowser wind...
Open a MySQL shell using credentials from database.yml
In order to open a MySQL shell without the need to enter user and password, you can say the following in any Rails 2 project:
script/dbconsole -p
In Rails 3 you can say:
rails dbconsole -p
If you'd like to enter a database for an environment other than development you can say:
script/dbconsole -p staging
MySQL: For each group, retrieve a comma-separated list of values in a given column
The technique described in this card has an important caveat: The result of GROUP_CONCAT is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. This will cause horrible, data-destroying bugs in production. For this reason you should probably not use GROUP_CONCAT ever. At least you must set the value of group_concat_max_len to an insanely high value on every database server your application runs on.
Lik...
Using heredoc for prettier Ruby code
You can use heredoc to avoid endlessly long lines of code that nobody can read. Heredoc strings preserve linebreaks and can be used like this:
def long_message
puts(<<-EOT)
Here goes a very long message...
Sincerely,
foobear
EOT
end
<<-EOT will be somewhat of a placeholder: anything you write in the line after you used it will be its value until you write EOT in a single line.
You can use any string to flag your heredocs. To be more verbose you...
Couldn't create database for ...
When you run rake db:create and get this error message
Couldn't create database for {"encoding"=>"utf8", "username"=>"root", "adapter"=>"mysql", "database"=>"project_development", "password"=>"topsecret"}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)
make sure the user you have specified (root/topsecret) in your database.yml has access to MySQL. You can check this by running mysql -uroot -p.
Solving "cannot remove Object::ClassMethods"
Most likely you run rake and your code is causing an exception which is not the one shown in your terminal.
Rails tries to catch this exception and clean up constants but -- while it's still booting up -- fails on this which causes another exception:
rake aborted!
cannot remove Object::ClassMethods
Running rake with the --trace parameter will give you no love; the backtrace is useless in most cases.
Try these approaches:
First: Check if there is a helpful error message
- Ha...
MySQL: How to clone a database
Here is a way to create a duplicate of one database, with all its tables and their data, under a new name.
-
Make a dump of your source database:
mysqldump -uroot -p my_project -r my_project.sqlOr, if you only want to dump the database's table structure (schema) without any contents:
mysqldump -uroot -p my_project -r my_project.sql --no-data -
Open up a MySQL shell:
mysql -uroot -p -
From the MySQL shell, create a new database and populate it with the dumped data:
CREATE DATABASE my_project_copy;
...