Polymorphic associations’ as and where is used in SQL

Problem

You are using a polymorphic association in your application, and you would like to know how exactly the as declaration in your model has_many is used in the SQL by ActiveRecord. So following the example in the Rails Guides with the following models:

 

class Picture < ActiveRecord::Base
  belongs_to :imageable, :polymorphic => true
end

class Employee < ActiveRecord::Base
  has_many :pictures, :as => :imageable
end

class Product < ActiveRecord::Base
  has_many :pictures, :as => :imageable
end

Solution

ActiveRecord uses the as in the model declaration as a guide to find the foreign id key in the polymorphic association, by adding the _id to the :as name. So following the
example above the query for the product would be:

SELECT `images`.*
FROM `images`
WHERE (`ratings`.imageable_id = 289
      AND `images`.imageable_type = 'Product')

Passenger (mod_rails) gem installation – Apache2 – Debian – rvm

Problem
You would like to (re)-install passenger after a system wide rvm installation, but you only have the mod_passenger.c file and not the .so after the gem installation.

Solution
Make sure that after using gem install passenger you also use the command to install the apache2 passenger module:

passenger-install-apache2-module

you should then be able to see the .so file and add it to your /etc/apache2/mods-available/passenger.load

Could not find generator jquery:install

Problem
When you try to replace prototype with jquery in your Rails 3.0.x application, by using the command described in the Agile Web development book:

rails generate jquery:install --ui --force

you get the following error:

Could not find generator jquery:install

Solution
You would need to follow the steps below:

  1. Add gem “jquery-rails”, “~> 1.0.13” in your Gemfile, and run bundle install
  2. run the command described above: rails generate jquery:install –ui –force

Now you should be able to see something like:

remove public/javascripts/prototype.js
remove public/javascripts/effects.js
remove public/javascripts/dragdrop.js
remove public/javascripts/controls.js
copying jQuery (1.6.2)
create public/javascripts/jquery.js
create public/javascripts/jquery.min.js
copying jQuery UI (1.8.14)
create public/javascripts/jquery-ui.js
create public/javascripts/jquery-ui.min.js
copying jQuery UJS adapter (cd619d)
remove public/javascripts/rails.js
create public/javascripts/jquery_ujs.js

Testing associations in Rspec

Problem
You would like to test your model associations in an RSpec model spec, to make sure that they are correctly set up.

Solution
After some Google searching and coming across this article here, ended up using the second method described there with the reflect_on_association method, as it doesn’t need the inclusion of another gem.
So for example to to test multiple has_many or belongs_to associations in your model rspec you could use something like:
context "check associations" do
# belongs_to associations
%w{bel_to1 bel_to2}.each do |bt|
it "should belong to #{bt}" do
m = Model.reflect_on_association(:"#{bt}")
m.macro.should == :belongs_to
end
end
# has_many associations
%w{has_many1 has_many2 has_many3}.each do |hm|
it "should have many #{hm}" do
m = Model.reflect_on_association(:"#{hm}")
m.macro.should == :has_many
end
end
end

The single-table inheritance mechanism failed to locate the subclass:

Problem
You have correctly setup your Single Table Inheritance tables as in:

class Parent < ActieRecord::Base ; end class Child < Parent ; end

but everytime you are trying to use that in a cucumber feature, or in rails console you get the following error:

The single-table inheritance mechanism failed to locate the subclass: 'child'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Parent.inheritance_column to use another column for that information. (ActiveRecord::SubclassNotFound)

Solution
Make sure that when use the subclass name in a feature, or you create a record in the database the initial letter is in capital!
So make sure that in this case the type column contains 'Child' and not 'child'

Google-Maps-Rails (gmaps4rails) does not display markers when upgrading to 0.10.0

Problem
When upgrading the gmaps4rails gem to version 0.10.0 the markers are not displayed any more on the map.

Solution
There is an issue raised about the bug here.
To make the markers appear downgrade to version 0.9.1 of the gem.

update: or upgrade to 0.10.2 as the bug is fixed.

ImageMagick, RMagick, Debian installation

Problem
You want to use the rmagick gem in your ruby on rails project, but you need to install the imagemagick first.

Solution
If Imagemagick is not already installed, use the following to install it:
$ sudo apt-get install imagemagick$ sudo apt get install libmagick-dev libmagickwand-dev

You should then be able to install and use the rmagick gem

ImageMagick, RMagick, Mandriva 64 installation

Problem
You want to use the rmagick gem in your ruby on rails project, but you need to install the imagemagick first.

Solution
If Imagemagick is not already installed, use the following to install it:
$ sudo urpmi imagemagick$ sudo urpmi lib64magick-devel

You should then be able to install and use the rmagick gem

Couldn’t find ‘rspec’ generator – Rails 2.3.10

Problem
You are trying to use rspec with Ruby on Rails 2.3.10, but although you install the rspec and rspec-rails gems you get the following error when you try to run script/generate rspec
Couldn't find 'rspec' generator

Solution
It turns out that there is a problem with the versions used, so by following the instructions for Rails >= 2.3.10 from here, you should be able to install the plugins and use rspec:
ruby script/plugin install git://github.com/dchelimsky/rspec.git -r 'refs/tags/1.3.1'
ruby script/plugin install git://github.com/dchelimsky/rspec-rails.git -r 'refs/tags/1.3.3'
ruby script/generate rspec

where the last tag (1.3.3) should be the latest tag for each gem.

*** NOTE *** If you get an error like :
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map (NoMethodError)

uncomment the following line (line 11) from features/support/env.rb:
require 'cucumber/rails/rspec'
and run rake cucumber again.