EDIT: This post has been updated and retitled for El Capitan.

 

If you have attempted to install the Nokogiri gem and encountered errors similar to the output below… I feel your pain and have a solution.

The Pain

➜ z gem install nokogiri Building native extensions. This could take a while... ERROR: Error installing nokogiri: ERROR: Failed to build gem native extension.
/Users/mariozig/.rvm/rubies/ruby-2.2.1/bin/ruby -r ./siteconf20150318-11691-1hlore5.rb extconf.rb
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
checking for gzdopen() in -lz... yes
checking for iconv using --with-opt-* flags... yes
************************************************************************
IMPORTANT NOTICE:
Building Nokogiri with a packaged version of libxml2-2.9.2
with the following patches applied:
 - 0001-Revert-Missing-initialization-for-the-catalog-module.patch
 - 0002-Fix-missing-entities-after-CVE-2014-3660-fix.patch
Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:
gem install nokogiri -- --use-system-libraries
 [--with-xml2-config=/path/to/xml2-config]
 [--with-xslt-config=/path/to/xslt-config]
If you are using Bundler, tell it to use the option:
bundle config build.nokogiri --use-system-libraries
 bundle install
Note, however, that nokogiri is not fully compatible with arbitrary
versions of libxml2 provided by OS/package vendors.
************************************************************************
Extracting libxml2-2.9.2.tar.gz into tmp/x86_64-apple-darwin14.1.0/ports/libxml2/2.9.2... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxml2/0001-Revert-Missing-initialization-for-the-catalog-module.patch...
Running 'patch' for libxml2 2.9.2... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxml2/0002-Fix-missing-entities-after-CVE-2014-3660-fix.patch...
Running 'patch' for libxml2 2.9.2... OK
Running 'configure' for libxml2 2.9.2... OK
Running 'compile' for libxml2 2.9.2... OK
Running 'install' for libxml2 2.9.2... OK
Activating libxml2 2.9.2 (from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/x86_64-apple-darwin14.1.0/libxml2/2.9.2)...
************************************************************************
IMPORTANT NOTICE:
Building Nokogiri with a packaged version of libxslt-1.1.28
with the following patches applied:
 - 0001-Adding-doc-update-related-to-1.1.28.patch
 - 0002-Fix-a-couple-of-places-where-f-printf-parameters-wer.patch
 - 0003-Initialize-pseudo-random-number-generator-with-curre.patch
 - 0004-EXSLT-function-str-replace-is-broken-as-is.patch
 - 0006-Fix-str-padding-to-work-with-UTF-8-strings.patch
 - 0007-Separate-function-for-predicate-matching-in-patterns.patch
 - 0008-Fix-direct-pattern-matching.patch
 - 0009-Fix-certain-patterns-with-predicates.patch
 - 0010-Fix-handling-of-UTF-8-strings-in-EXSLT-crypto-module.patch
 - 0013-Memory-leak-in-xsltCompileIdKeyPattern-error-path.patch
 - 0014-Fix-for-bug-436589.patch
 - 0015-Fix-mkdir-for-mingw.patch
Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:
gem install nokogiri -- --use-system-libraries
 [--with-xml2-config=/path/to/xml2-config]
 [--with-xslt-config=/path/to/xslt-config]
If you are using Bundler, tell it to use the option:
bundle config build.nokogiri --use-system-libraries
 bundle install
************************************************************************
Extracting libxslt-1.1.28.tar.gz into tmp/x86_64-apple-darwin14.1.0/ports/libxslt/1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0001-Adding-doc-update-related-to-1.1.28.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0002-Fix-a-couple-of-places-where-f-printf-parameters-wer.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0003-Initialize-pseudo-random-number-generator-with-curre.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0004-EXSLT-function-str-replace-is-broken-as-is.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0006-Fix-str-padding-to-work-with-UTF-8-strings.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0007-Separate-function-for-predicate-matching-in-patterns.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0008-Fix-direct-pattern-matching.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0009-Fix-certain-patterns-with-predicates.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0010-Fix-handling-of-UTF-8-strings-in-EXSLT-crypto-module.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0013-Memory-leak-in-xsltCompileIdKeyPattern-error-path.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0014-Fix-for-bug-436589.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0015-Fix-mkdir-for-mingw.patch...
Running 'patch' for libxslt 1.1.28... OK
Running 'configure' for libxslt 1.1.28... ERROR, review '/Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ext/nokogiri/tmp/x86_64-apple-darwin14.1.0/ports/libxslt/1.1.28/configure.log' to see what happened.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
 --with-opt-dir
 --with-opt-include
 --without-opt-include=${opt-dir}/include
 --with-opt-lib
 --without-opt-lib=${opt-dir}/lib
 --with-make-prog
 --without-make-prog
 --srcdir=.
 --curdir
 --ruby=/Users/mariozig/.rvm/rubies/ruby-2.2.1/bin/$(RUBY_BASE_NAME)
 --help
 --clean
 --use-system-libraries
 --enable-static
 --disable-static
 --with-zlib-dir
 --without-zlib-dir
 --with-zlib-include
 --without-zlib-include=${zlib-dir}/include
 --with-zlib-lib
 --without-zlib-lib=${zlib-dir}/lib
 --enable-cross-build
 --disable-cross-build
/Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:279:in `block in execute': Failed to complete configure task (RuntimeError)
 from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:271:in `chdir'
 from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:271:in `execute'
 from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:66:in `configure'
 from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:109:in `cook'
 from extconf.rb:278:in `block in process_recipe'
 from extconf.rb:177:in `tap'
 from extconf.rb:177:in `process_recipe'
 from extconf.rb:487:in `<main>'
extconf failed, exit code 1
Gem files will remain installed in /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /Users/mariozig/.rvm/gems/ruby-2.2.1/extensions/x86_64-darwin-14/2.2.0/nokogiri-1.6.6.2/gem_make.out

The Problem

For some reason Apple’s Yosemite version of OSX does not have a system accessible installation of libxml2.  Nokogiri requires this in order to compile and luckily Xcode has a version of libxml2 bundled with it — we just need to specify it when installing the gem.  It’s important to get Nokogiri installed correctly because as of right now Rails 4.2.1.rc4 automatically attempts to install it and you will feel pain.

How I Solved It

gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2/

Some Preface

An app I’m working on has a rake that that basically gathers data from the web.  Doing things serially is really REALLY slow and to get around this I started using the (fantastic) parallel gem by Michael Grosser.

This gem is great — it’s simple to use and in something like 3 minutes after installing it I had my task running in 10 processes and absolutely murdering the work that needed to be done.

The Issue

I swapped databases not too long ago and moved from MySQL to PostgreSQL for full text search (which i don’t use anymore) and to stay in line with my host, Heroku.

Today i attempted to run the task against my PostgreSQL db and came up with an error I had never seen before:

message type 0x5a arrived from server while idle

WHUT?

After some investigating on Google I found the root cause — PostgreSQL does not allow using the same connection for more than one thread.

That’s pretty straight forward.

I believe the issue was that I had something like 10 ruby processes that were spawned from 1 process that was holding the db connection.  Not allowed!

The Solution

The solution is actually very straight forward as well.  To get around this you simply need to reconnect to the database each time you spawn a process or thread.

What does this look like in code?

Before (bad) :

After (GOOD):

 

And that’s pretty much it.

I’m using Twitter’s Bootstrap CSS framework and it’s pretty nice.  One of the cool bundled features of the framework is that they provide classes for displaying what rails hackers know as Flash messages.  The problem here is that Twitter has named them different from what Rails people traditionally use so you cannot just drop in Bootstrap and expect your flash messages to appear properly.

I did NOT want to modify the original bootstrap css and tack on extra classes so I got around this with an application helper method that does some translating for me.

Here is the code:

 

Yeah, this post has the WORST title ever, but i tried to litter it with keywords so people struggling can find it easily.

When messing with my Rails views I find that pretty commonly I’m dealing with lists of things i need to use link_to on AND split them with a comma.  Here’s a super simple trick that will help you keep your view cleaner (this is for Haml).  For the sake of this tiny code snippet pretend you are trying to create a list of your recent posts separated by commas:

= raw @recent_posts.collect { |post| link_to post.title, post }.join(",")

By using collect you iterate over your array of recent posts and build each one into a link using the ‘link_to’ helper — THEN just join with a comma and BAM you have a nice html string of links.  Prefix the whole thing with ‘raw’ because if you don’t Rails will emit escaped HTML in your face.

I talked to a gentleman in #rubyonrails on IRC who goes by the name of codepoet_ about Rails3 and Arel for a while today and he linked me to what is somewhat of an Arel cheat sheet he created.

With his permission I am reposting this as a reference tool for myself.  Hopefully someone else can benefit!
[sourcecode language=’ror’]
# In a User model:

scope :men, where(“sex = ?”, ‘M’)
or
scope :men, where(:sex => ‘M’)

scope :in_tights, where(“clothing_preference = ?”, ‘Tights’)
or
scope :in_tights, where(“clothing_preference, ‘Tights’)

scope :created_in_first_week_july_this_year, where(“DATE(created_at) BETWEEN ? AND ?”, Date.parse(“July”), Date.parse(“July”) + 7.days)

scope :with_dark_hair, where(“(hair_color = ? OR ?)”, “black”, “brown”)

scope :oldest_first, order(“age DESC”)

scope :oldest, oldest_first.limit(1)

scope :not_cancelled, where(“status != ‘Cancelled'”)

scope :old_dark_men_in_tights_for_july, men.in_tights.with_dark_hair.created_in_first_week_july_this_year

# get the men

User.men

#get the men in tights (these two do the same thing)
User.men.in_tights
User.in_tights.men

# get the men, order by the oldest first
User.men.oldest_first

# get the oldest man
User.men.oldest

# get the oldest man who has not been cancelled
User.men.oldest.not_cancelled

# get the users created in the first week of july
User.created_in_first_week_july_this_year

# get the dark hair colored men in tights created in the first week of july
User.men.in_tights.with_dark_hair.created_in_first_week_july_this_year

# Or you can use the last scope which references other scopes, to cut down on the tedium of chaining so many things:
User.old_dark_men_in_tights_for_july
[/sourcecode]

I ran into an issue today.  I have a Blog model that has_many feed_entries.

In plain english, I needed a list of blogs ordered by most recently updated feed.

My models look like this:

blog.rb
has_many :feed_entries

feed_entry.rb
belongs_to :blog

The query that makes it possible:

Blog.find(:all, :include => :feed_entries, :order => 'feed_entries.published_at DESC')

:include makes this possible!

This is how i got Google Maps working in Radiant CMS via the google_maps extension.

This one is pretty easy, but you should know that you HAVE to be using MySQL or PostgreSQL because google_maps uses the spatial adapter plugin which needs one of those 2.

First install the GeoRuby gem.  Careful this is case sensitive:
sudo gem install GeoRuby

Next, from within your Radiant project’s root folder, clone the extension from github:
git clone git://github.com/rurounijones/radiant-google-maps-extension.git vendor/extensions/google_maps

Time to run the Rake tasks:
rake radiant:extensions:google_maps:api
rake production radiant:extensions:google_maps:migrate
rake production radiant:extensions:google_maps:update

Edit config/gmaps_api_key.yml and add your own Google Maps API key.

Add this R tag inside your layout’s head tag:
<r:google_map:header />

Goto the Maps tab in the Radiant Admin and create a map

Create a new page and name it something.  In the body of that page add:
<r:google_map:generate div=”my_gmap_div_name” id=”1″ />
<div id=”my_gmap_div_name” style=”height: 600px; width: 868px;”></div>

That provides a div for the map to go in, and calls the google_map generate tag with Map id 1.  Look on the github page for more stuff.

This post is more of a personal note to myself. 99% is copy/paste from either the Radiant wiki, or the Mailer extension’s github page. I’m notating this because both the Radiant wiki and Mailer’s github page have instructions that confused the hell out of me so I’m consolidating it here after getting it working.

This tutorial assumes you have a freshly bootstrapped Radiant installation. Let’s Go!Continue reading