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')

Cucumber Table transformations with Factory Girl

Problem
After reading this post here, to be able to use the Cucumber table trasformatios feature to build objects in your cucumber tests, you would like to use FactoryGirl instead of the standard model.
The reason for using FactoryGirl could be that your model needs a few more attributes (mandatory fields), that you don’t want to specify in your cucumber table, but you want the factory to take care of them.

Solution
The only difference would take place in the transformation step.
So if you would originally have the following:

Transform /^table:Vehicle,User,Start,End$/ do |table|
  table.hashes.map do |hash|
    vehicle = Vehicle.create!({:regno => hash['Vehicle']})
    user = User.create!({:first_name => hash['User']})
    booking = Booking.create!({:start_at => hash['Start'],
                               :end_at => hash['End']})
    {:vehicle => vehicle, :user => user, :booking => booking}
  end
end

and for example your user has also fields like password, date of birth, that are mandatory in the model validations, you would put them in your factory define declaration like:

Factory.define(:user) do |u|
u.first_name "Name"
u.last_name "Lastname"
u.address1 "Address1"
u.address2 "Line 2"
u.address3 "Line 3"
u.zip "1111"
u.city "City"
u.sequence(:email) {|n|"teste#{n}@test.com"}
u.profile_picture File.open(File.join(Rails.root,"features/fixtures/user.png"))
u.password "password"
end

and replace the tranformation to:

 

Transform /^table:Vehicle,User,Start,End$/ do |table|
table.hashes.map do |hash|
vehicle = Factory(:vehicle, :regno => hash['Vehicle'])
user = Factory(:user,:first_name => hash['User'])
booking = Factory(:booking, :start_at => hash['Start'],
:end_at => hash['End'])

{:vehicle => vehicle, :user => user, :booking => booking}
end
end

The actual step would similar to the one in the blog article:

Given /^the following bookings?:$/ do |table|
# table is a Cucumber::Ast::Table
table.each do |group|
booking = group[:booking]
associations = {:vehicle => group[:vehicle], :user => group[:user]}
booking.update_attributes(associations)
end
end

Secure Random passwords in Ruby

Problem
You would like to use a random password in ruby. Most solutions describe using the Digest library (Digest::SHA1).

Solution
According to the post here, you can achieve the same by using ActiveSupport’s secure_random library.
To use it in Ruby outside Rails use:

require 'active_support/secure_random'
ActiveSupport::SecureRandom.hex(10)
ActiveSupport::SecureRandom.base64(10)


and inside rails:

SecureRandom.hex(10)

passenger ArgumentError (invalid byte sequence in US-ASCII)

Problem
After upgrading your server to use rvm and Ruby 1.9 you get the following error when you are trying to access certain parts of your redmine installation:

passenger ArgumentError (invalid byte sequence in US-ASCII)

Solution
If you follow the instruction posted here, you can add the following to your redmine’s “RAILS_ROOT/config/initializers/string_encodings.rb”:

Encoding.default_external = 'UTF-8'

and restart your application

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

multiple user installation of rvm in Debian

Problem
You would like to install rvm as a multiple user installation in Debian.

Solution
You would need to do the following:

as root

  • bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

for each individual user you want to use rvm

  • in the user’s .bashrc or .bashrc_profile add the following at the bottom:
    [[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm" # This loads RVM into a shell session.
  • reload the .bashrc or .bashrc_profile by:
    source .bashrc|.bashrc_profile

and you should be able to do rvm -v.