Active Record Find Methods

Active Record find methods for selecting range from http://charlesmaxwood.com/notes-from-reading-activerecordbase/

Student.find(:all, :conditions => { :grade => 9..12 })
return a range
Student.find(:all, :conditions => { :grade => [9,11,12] })
will return an "in()"

Carrier Email Addresses for Sending SMS over Email

Just for reference, here are the carrier email addresses for sending email as an SMS. Look up the carrier for the phone in question, then send an email in this format [telephonenumber]@[carrier-name.com]

Carrier Email to SMS Gateway

Alltel [10-digit phone number]@message.alltel.com
Example: 1234567890@message.alltel.com
AT&T (formerly Cingular) [10-digit phone number]@txt.att.net
[10-digit phone number]@mms.att.net (MMS)
[10-digit phone number]@cingularme.com
Example: 1234567890@txt.att.net
Boost Mobile [10-digit phone number]@myboostmobile.com
Example: 1234567890@myboostmobile.com
Nextel (now Sprint Nextel) [10-digit telephone number]@messaging.nextel.com
Example: 1234567890@messaging.nextel.com
Sprint PCS (now Sprint Nextel) [10-digit phone number]@messaging.sprintpcs.com
[10-digit phone number]@pm.sprint.com (MMS)
Example: 1234567890@messaging.sprintpcs.com
T-Mobile [10-digit phone number]@tmomail.net
Example: 1234567890@tmomail.net
US Cellular [10-digit phone number]email.uscc.net (SMS)
[10-digit phone number]@mms.uscc.net (MMS)
Example: 1234567890@email.uscc.net
Verizon [10-digit phone number]@vtext.com
[10-digit phone number]@vzwpix.com (MMS)
Example: 1234567890@vtext.com
Virgin Mobile USA [10-digit phone number]@vmobl.com
Example: 1234567890@vmobl.com
Free Email To SMS Gateways (International + Smaller US)

These are all I could find from Wikipedia and other sources. If you’re aware of any other ones please share them in comments and I’ll add them to the list.

Carrier Email to SMS Gateway
7-11 Speakout (USA GSM) number@cingularme.com
Airtel (Karnataka, India) number@airtelkk.com
Airtel Wireless (Montana, USA) number@sms.airtelmontana.com
Alaska Communications Systems number@msg.acsalaska.com
Aql number@text.aql.com
AT&T Enterprise Paging number@page.att.net
BigRedGiant Mobile Solutions number@tachyonsms.co.uk
Bell Mobility & Solo Mobile (Canada) number@txt.bell.ca
BPL Mobile (Mumbai, India) number@bplmobile.com
Cellular One (Dobson) number@mobile.celloneusa.com
Cingular (Postpaid) number@cingularme.com
Centennial Wireless number@cwemail.com
Cingular (GoPhone prepaid) number@cingularme.com (SMS)
Claro (Brasil) number@clarotorpedo.com.br
Claro (Nicaragua) number@ideasclaro-ca.com
Comcel number@comcel.com.co
Cricket number@sms.mycricket.com (SMS)
CTI number@sms.ctimovil.com.ar
Emtel (Mauritius) number@emtelworld.net
Fido (Canada) number@fido.ca
General Communications Inc. number@msg.gci.net
Globalstar (satellite) number@msg.globalstarusa.com
Helio number@messaging.sprintpcs.com
Illinois Valley Cellular number@ivctext.com
Iridium (satellite) number@msg.iridium.com
Iusacell number@rek2.com.mx
i wireless number.iws@iwspcs.net
Koodo Mobile (Canada) number@msg.koodomobile.com
LMT (Latvia) number@sms.lmt.lv
Meteor (Ireland) number@sms.mymeteor.ie
Mero Mobile (Nepal) 977number@sms.spicenepal.com
MetroPCS number@mymetropcs.com
Movicom (Argentina) number@sms.movistar.net.ar
Mobitel (Sri Lanka) number@sms.mobitel.lk
Movistar (Colombia) number@movistar.com.co
MTN (South Africa) number@sms.co.za
MTS (Canada) number@text.mtsmobility.com
Nextel (United States) number@messaging.nextel.com
Nextel (Argentina) TwoWay.11number@nextel.net.ar
Orange Polska (Poland) 9digit@orange.pl
Personal (Argentina) number@alertas.personal.com.ar
Plus GSM (Poland) +48number@text.plusgsm.pl
President’s Choice (Canada) number@txt.bell.ca
Qwest number@qwestmp.com
Rogers (Canada) number@pcs.rogers.com
SL Interactive (Australia) number@slinteractive.com.au
Sasktel (Canada) number@sms.sasktel.com
Setar Mobile email (Aruba) 297+number@mas.aw
Suncom number@tms.suncom.com
T-Mobile (Austria) number@sms.t-mobile.at
T-Mobile (UK) number@t-mobile.uk.net
Telus Mobility (Canada) number@msg.telus.com
Thumb Cellular number@sms.thumbcellular.com
Tigo (Formerly Ola) number@sms.tigo.com.co
Tracfone (prepaid) number@mmst5.tracfone.com
Unicel number@utext.com
Virgin Mobile (Canada) number@vmobile.ca
Vodacom (South Africa) number@voda.co.za
Vodafone (Italy) number@sms.vodafone.it
YCC number@sms.ycc.ru
MobiPCS (Hawaii only) number@mobipcs.net

Why are PHP5 Namespaces Defined Using a Backslash?

Why are PHP namespaces defined using a backslash? It looks ugly. Unless of course, there is a good reason for the “\”? Does this namespaced code run more efficiently on Windows?

Since namespaces are new in PHP5 why not take the opportunity to use them when requiring a file?

namespace('my_library_dir', 'lib'); // import my_library_dir as lib
$myClass = new lib::MyClass;      // use the class w/ namespace

Seems a lot simpler and not as ugly. You could namespace old PHP code this way as well. Please enlighten me if I’m off the mark and the implementation of namespaces in PHP5 is a better design than I have suggested!

Very Basic Git Workflow

Very, very basic git workflow

git pull
git branch dev_branch
git checkout dev_branch
#make some changes
git checkout master
git merge dev_branch
git branch -d branch_to_delete
git push

Using Module Mixins to Extend Classes and Objects in Ruby

The module and class below demonstrate how to use instance methods and class methods from “module mixins”. The thing to remember is scope. For instance, to use class methods within instance methods you need to call them from the class itself. This is accomplished something like this “self.class.class_method_you_want_to_call”. The example below should make it more clear. It would be natural to assume using “self.method_name” within your module mixin, because after all, instance methods just work when included this way. However, you need to take an extra step when you want to “extend” the behavior of a class.

To extend a class with a mixin you use the method “self.included(base)”. This method takes the parent class “base” as an argument. In the method body you use base.extend to attach class methods. Wrapping up class methods in a module is the easiest way to do it.

module Greeting
  # Instance Methods
  # Usage Martian.new.hello_world
  def hello_world
    "Martian says: Hello " + self.class.planet+"!"
  end

  # Class Methods
  # Usage: Martian.planet
  def self.included(base)
    base.extend ClassMethods
  end
  module ClassMethods
    def planet
      "world"
    end
  end
end

class Martian
  include Greeting
  def self.location
    "Martian lives on "+self.planet
  end
end

p Martian.new.hello_world
p Martian.location

Installing RedMine PM Software on Apache with Phusion and REE and Seeing a 404 Page Not Found Error on Installation

If you follow the instructions on how to install the Rails Redmine PM Software (available here) and are using Apache with Passenger (REE), you need to delete the .htaccess file that is kept in RAILS_ROOT/public directory. Otherwise you’ll see a 404 page not found error. Took me a while to hunt this down. I stopped thinking about .htaccess in Rails apps but I guess REE+Passenger isn’t the default deployment yet.

Change database column names for form validations in Rails

When you use validations in Rails, db column names are used as ‘keys’ for error messages. This is usually the preferred way to go about it because this maps nicely to the form fields. However, if you use a virtual attribute this may not be the case. For example, I have a ‘password_crypted’ field in my users table that I don’t want my user to see if they fail to complete the field. Instead of returning “Password crypted cannot be blank” I just want to tell them that a password can’t be blank. If you provide a custom ‘:message’ on the validation this won’t replace the column name. The solution is to override the “human_attribute_name” class method and map specific column names to the string you want to use instead.

class User < ActiveRecord::Base
  validates_presence_of :password_crypted
  ATTR_NAMES = {:password_crypted => "Password"}
  def self.human_attribute_name(attr)
     ATTR_NAMES[attr.to_sym] || super
  end
end

I found these resources helpful while I was in search for a solution to this problem.

http://stackoverflow.com/questions/808547/fully-custom-validation-error-message-with-rails

http://henrik.nyh.se/2007/12/change-displayed-column-name-in-rails-validation-messages

Defining Application Constants for Ruby on Rails Application

The best place to keep application constants which are environment specific is in config/environments directory. For instance…

# in RAILS_ROOT/config/environments/development.rb
APP_DOMAIN = "localhost"
# in RAILS_ROOT/config/environments/production.rb
APP_DOMAIN = "real-domain.com"

…will set the APP_DOMAIN constant to either “localhost” or “real-domain.com” depending on which environment Rails boots up.

Adding Public/Private Key Pairs on Mac OS X and Ubuntu for Passwordless Remote SSH Sessions

On your local machine cd into the .ssh directory in your home “~/” directory. If it doesn’t exist you can create it with “mkdir ~/.ssh”. Next generate your public/private keys and copy the public key to the remote server.

cd ~/.ssh
ssh-keygen -t rsa -b 4096
# will take a couple seconds but when finished
# specify a full path (if there is already an existing key) or hit enter to install to the default location ~/.ssh
# when it prompts for a passphrase just hit enter
# and enter again when it asks to confirm the passphrase
# then we copy the public key the remote server (this assumes you don't already have an authorized_keys file)
# copy and paste the contents of the id_rsa.pub file into the authorized_keys file otherwise
scp id_rsa.pub user@yourdomain.com:.ssh/authorized_keys

You’ll need to edit your ssh config file and restart the process to allow for public/private key authentication.

vim /etc/ssh/ssh_config
# add or uncomment these two lines
RSAAuthentication yes
PubKeyAuthentication yes
# ... and restart
/etc/init.d/ssh restart

Troubleshooting

A couple of things to keep in mind. 1) Permissions matter. Make sure that your keys are not world readable (this should be secure) Run chmod 400 on authorized_keys file.

If you had a set of keys already setup in .ssh/ on your local machine and want to install the new keys in another directory so as not to overwrite the old pair, you need to add them to ssh with this command

ssh-add ~/full/path/to/your/new/keys

More information is available here http://www.debian-administration.org/articles/152

Hacking Rails Plugins

Using the Acts as Taggable On plugin to add categories to a model class, I wanted to override the to_param method and place the name attribute in the url. The plugin, installed as a gem, source shouldn’t need to be hacked in order to accomplish this. The solution is to add a plugin into the RAILS_ROOT/vendor/plugins directory and append the plugin name with _hack. This will, because of an alphabetical load order, allow you to reopen the any class in the plugin. In my example

# RAILS_ROOT/vendor/plugins/acts_as_taggable_on_hack/init.rb
Tag.class_eval do
  to_param
    "#{id}-#{name.gsub(/[^a-z0-9]+/i, '-'/)
  end
end

Got this tip from http://errtheblog.com/posts/67-evil-twin-plugin.