byebug / ruby-debug: Find out current debugger position
So you are debugging like a boss and lost track of where you actually are in your code? No problem:
- Calling "
l=" will show you the current file and line. That's a lower-caseLand an equals sign. - "
where" (or "backtrace") will give you the debugger call stack, including current file and line as well. It can be quite long.
Zeus promises to make rails development faster
I am talking about development speed. When your application starts growing and you start adding gems, it starts to take really long to start up, be it the server, console or just running a single spec.
Zeus is smart, you don’t have to put it in your Gemfile or run it with Bundler, all you need to do is create a JSON config file via
zeus initand then start the serverzeus start.
After that, you’re ready to go, all you need to do is prefix every command with zeus. That means
rails serverbecomeszeus server, `rails console...
Capybara 2.0 has been released
The gem author Jonas Nicklas highlights in a Google Groups post that the release
- is not backwards compatible to 1.x versions of Capybara
- does not support Ruby 1.8.x anymore
- removes confusion with Rails' built in integration tests (you put capybara rspec integration tests into the
spec/feature/...folder) and the:typemetadata has been changed from:requestto:feature - throws exceptions when trying to interact with an element whose identifier is...
Andand and SimpleDelegator
The very useful andand gem does not play very nice with Ruby's SimpleDelegator (or vice versa).
This following will not work:
class MyDecorator < SimpleDelegator
def foo
end
end
MyDecorator.new(Object.new).andand.foo
The reasons are a bit subtle, basically SimpleDelegator will "force" some methods to be delegated, so the andand method is called on the wrapped object, not the delegator.
You can fix it like this:
class Decorator < SimpleDelegator
def an...
Using sets for many-to-many relationships
A technique to vastly reduce the number of join model records that need to be stored in the database.
The technique is only effective when there is a high redundancy in your data, e.g. combinations of the same 20 tags are used to label thousands of books.
The technique is also limited in that your join models cannot have additional logic, such as attributes or callbacks.
Ther has-many-with-set gem is an implementation of this technique.
uninitialized constant MysqlCompat::MysqlRes (NameError)
If you get a stacktrace complaining about uninitialized constant MysqlCompat::MysqlRes a system library update might broke your gem.
You might have switched from MySQL to MariaDB, but forgot to rebuild your MySQL gems.
Try fully removing and re-installing the gem:
gem uninstall mysql mysql2
bundle install
Rails: When to use :inverse_of in has_many, has_one or belongs_to associations
When you have two models in a has_many, has_one or belongs_to association, the :inverse_of option in Rails tells ActiveRecord that they're two sides of the same association.
Example with a has_many / belongs_to association:
class Forum < ActiveRecord::Base
has_many :posts, inverse_of: :forum
end
class Post < ActiveRecord::Base
belongs_to :forum, inverse_of: :posts
end
Knowing the other side of the same association Rails can optimize object loading so forum and forum.posts[0].forum will reference the same o...
Capybara: evaluate_script might freeze your browser
Capybara gives you two different methods for executing Javascript:
page.evaluate_script("$('input').focus()")
page.execute_script("$('input').focus()")
While you can use both, the first line (with evaluate_script) might freeze your browser window for 10 seconds.
The reason is that evaluate_script will always return a result. The return value will be converted back to Ruby objects, which in case of complex objects (e.g. a jQuery collection) is very expensive.
Because of this we recommend to only use evaluate_script whe...
Rubygems: Rebuild native extensions
Rarely, you might want to rebuild all gems with native extensions, because they might be compiled against outdated system libraries, resulting in some warnings or even segfaults or other ruby errors.
You can do that using
gem pristine --all
This will reset all gems to a pristine state as if you'd reinstall them, and as a side effect, rebuild all native extensions.
The above command will also help you sorting out errors like this after a distribution upgrade:
libmysqlclient_r.so.16: cannot open shared object file: No such fil...
Waiting for page loads and AJAX requests to finish with Capybara
If you're using the Capybara webdriver, steps sometimes fail because the browser hasn't finished loading the next page yet, or it still has a pending AJAX request. You'll often see workarounds like
When I wait for the page to load
Then ...
Workarounds like this do not work reliably, will result in flickering tests and should be avoided. There is no known reliable way to detect if the browser has finished loading the page.
Solution
Instead you should wait until you can observe the result of a page load. E.g. if y...
Ruby: Extract the hostname from a URL
url = 'http://www.foocorp.com/foo/bar'
URI.parse(url).host
# => www.foocorp.com
Note that this will raise an error if the given argument is not a URL.
If you need the host's full URL without path, query, fragment etc., use URI.join with a clever twist:
url = 'http://www.foocorp.com:33546/foo/bar?query=foobar#hash'
URI.join url, '/'
# => http://www.foocorp.com:33546/
Advice: Reduce scopes with joins to simple IN-queries
In theory you can take any scope and extend it with additional joins or conditions. We call this chaining scopes.
In practice chaining becomes problematic when scope chains grow more complex. In particular having JOINs in your scope will reduce the scope's ability to be chained with additional JOINs without crashes or side effects. This is because ActiveRecord doesn't really "understand" your scope chain, it only mashes together strings that mostly happen to look like a MySQL query in the end.
**I don't generally advice against u...
Manually requiring your application's models will lead to trouble
In a nutshell:
If you require your Rails models manually, pay attention to the path you use. Unless you have to, don't do it at all.
Background
Consider these classes:
# app/models/user.rb
class User < ActiveRecord::Base
validate :magic
def magic
errors.add_to_base('failed') if bad_things?
end
end
^
# app/models/foo.rb
require 'user'
class Foo
# something happens here
end
Now, when your environment is booted, Rails will automatically load your models, like User...
floere/phony
The (admittedly crazy) goal of this Gem is to be able to format/split all phone numbers in the world.
Fixing errors with state_machine when excluding states
This is for you if you get the following strange error from the state_machine gem:
undefined method `-' for #<StateMachine::BlacklistMatcher:0x124769b0>
You probably did something like this in your state_machine ... do block:
def self.final_states
[ :foo, :bar ]
end
transition (all - machine.final_states - [:baz]) => :target_state
Instead, define the source states like this:
def self.final_states
[ :foo, :bar ]
end
transition (all - (mach...
Literally 101 Ruby tricks
The linked slidedeck holds many tips, of which I list the most interesting to me below
DATA and END
The __END__ keyword tells Ruby where a file ends – but you don't have to stop there. Any text you add after it is accessible via the DATA object.
Running the attached example file data.rb prints:
DATA is a File object to everything in the current file after "__END__"
No braces needed for 'special variables'
@instance, @@class, $global = [ 'instance', 'class', 'global' ]
puts "#@instance, #@@class, #$global"
...
How Ruby lets you keep script and data in *one* file
The __END__ keyword tells Ruby where a file ends – but you don't have to stop there. Any text you add after it is accessible via the DATA object.
The attached example file data.rb looks like this:
puts DATA.read
__END__
DATA is a File object to everything in the current file after "__END__"
Running it with ruby data.rb prints:
DATA is a File object to everything in the current file after "__END__"
Geordi: Running Selenium tests in a VNC buffer
Geordi now supports our solution for running Selenium tests without having Firefox or Chrome windows popping up all over your workspace.
This will stop Selenium windows from appearing on your desktop, but you can still inspect them when necessary.
Installation
Update geordi with gem install geordi.
Run geordi vnc --setup and follow the instructions.
Usage
geordi cucumber will automatically use VNC. Launchy will still open pages in the usual place.
geordi vnc will allow...
Workaround for broken integer division after requiring the mathn library
Ruby's mathn library changes Fixnum division to work with exact Rationals, so
2 / 3 => 0
2 / 3 * 3 => 0
require 'mathn'
2 / 3 => Rational(2,3)
2 / 3 * 3 => 2
While this might sometimes be quite neat, it's a nightmare if this gets required by some gem that suddenly redefines integer division across your whole project. Known culprits are the otherwise excellent distribution and [GetText](https://g...
Get rid of WARNING: Nokogiri was built against LibXML version 2.7.7, but has dynamically loaded 2.7.8
If you get this warning on your local machine one of these steps might help:
Rebuilt the gem with the newer library
gem install --no-rdoc --no-ri nokogiri -- --with-xml2-include=/opt/local/include/libxml2 --with-xml2-lib=/opt/local/lib
If you still get the error, try to uninstall all nokogiri versions with
gem uninstall nokogiri
and install nokogiri again.
Fixing the issue on servers
However, on our servers this probably will not work. On the server, gems are stored in the ../shared/bundle/ruby/:version/gems dire...
Asset pipeline may break Javascript for IE (but only on production)
If some of your JavaScripts fail on Internet Explorer, but only in staging or production environments, chances are that JavaScript compression is the culprit.
By default, Rails 3.2 compresses JavaScript with UglifyJS. I have seen a few cases where this actually breaks functioning JavaScript on IE (one example is the CKEditor).
I fixed this by switching to Yahoo's YUI Compressor.
To do this, do the following:
- replace the
uglifiergem with theyui-compressorgem...
Migrating to Spreewald
This describes how to migrate an existing cucumber test suite to Spreewald.
-
Add the gem
-
Include spreewald into your cucumber environment by putting
require 'spreewald/web_steps'
require 'spreewald/email_steps'
# ...
or just
require 'spreewald/all_steps'
into yoursupport/env.rb. -
Look through your step definitions for everything that might be included in Spreewald. Candidates are
web_steps,shared_steps,table_steps, `em...
Spreewald: Old-school cucumber steps, freshly pickled
Cucumber_rails' old-school web-steps have been deprecated for a while, urging developers to write high-level step definitions that directly use Capybara or Webrat.
We think that's a bit drastic. More high-level steps are good, but ticking the odd check box with a general step is not always bad.
So we took the old web steps, improved them a bit, added some other favorites of ours (steps for emails, tables, [time travelling](/ma...
ActionMailer sometimes breaks e-mails with multiple recipients in Rails 2
The ActionMailer in Rails 2 depends on a buggy version of TMail, which sometimes inserts a blank line into the mail header when sending a mail to multiple recipients. This makes the header end prematurely.
The reason why this is not exploding in your face all the time is that when you are relaying your e-mail through an MTA like Exim, it will fix this for you.
Fix for Rails if you don't have an awesome MTA
TMail is no longer maintained. The bug is fixed...