Monthly Archives: November 2009

Ruby on Rails: Get some random records

EDIT AGAIN: Much better..

To get a ‘num_reqd’ array of random objects, you can use something like this.

  named_scope :large, :conditions => ['image_file_name IS NOT ?', nil]
  named_scope :small, :conditions => ['small_image_file_name IS NOT ?', nil] 

  def self.get(num_reqd,features_arr=[],size="large")
    if size=="small"
      collection = Feature.small
    elsif size=="large"
      collection = Feature.large
    end
    
    return collection if collection.size <= num_reqd

    # num_reqd.times{feature=self.random(collection); features_arr.push(feature) unless features_arr.include?(feature)}
    features_arr = collection.find(:all, :limit => num_reqd, :order => 'rand()')

    if features_arr.size < num_reqd
      return Feature.get(num_reqd, features_arr, size)
    else
      return features_arr
    end
  end

EDIT: It's much cleaner and easier to use something in the form below, though the following is probably useful in some cases and is possibly interesting as a code snippet.

User.find(:all, :order => 'rand()')


---- end edit.

Working from a baseline of the code found here at almosteffortless.com I've extended a 'random record grabber' to get a specific number of unique records from a Rails data table.

Basically - the random method makes a database call to get the ids of a table, and sends back a random entry. self.get is a recursive method which provides a 'total number required' and a base array to start from (if you wish to specify entries to appear in the otherwise 'random' list). First year computer science should help get your head around the rest!

def self.random
    ids = connection.select_all("SELECT id FROM features")
    find(ids[rand(ids.length)]["id"].to_i) unless ids.blank?
  end

  def self.get(num_reqd,features_arr=[])
    num_reqd.times{feature=self.random; features_arr.push(feature) unless features_arr.include?(feature)}

    if features_arr.size < num_reqd
      return Feature.get(num_reqd, features_arr)
    else
      return features_arr
    end
  end

Be aware, there is more efficiency to be found in the database call (i.e. it should be cached). Also, you'll want to be sure there are at least 'num_reqd' items in the database.

Using another computer for your heroku app

Quite easy, but without knowing how would you know… (the docs!)

The Collaborator Quick Start guide is the place to be.

[sudo] gem install heroku
# then
heroku keys:add
#your login details are taken and ssh key uploaded automatically
git clone git@heroku.com:APPNAME.git -o production
#using the proper git repo name.git

if git push heroku fails then do this:
git remote -v
Hopefully you will see an origin or heroku

If not, find out your git url:
heroku list
heroku info --app your-app-name

git remote add heroku git-url
(this bit from google groups)

Leopard Mac Refuses to Stay Asleep…

The last week or so my Leopard iMac has been waking up in the middle of the night… it’s quite annoying.

Finally found the answer here:
macrumors – leopard imac waking from sleep issue.

It turns out the problem is caused by having Screen Sharing enabled, and the 5900 port (VNC) being exposed to the outside world through my router. While this allows me to remote connect from another Mac ….

The problem lies in the fact that the screen comes to life whenever ANYTHING connects to this port, be it another Mac, or a port scanner… before even authentication takes place. Tested this by trying to SSH to that port – bang, my monitors come to life.

Fixed it by removing the external port forward, and just using SSH into the Mac. Apple needs to stop waking up the screens until authentication has taken place.

Basically this means that the really awesome TouchPad app that got Fireballed a few weeks ago is only really useful if you turn off your computer (completely) at night or are willing to manually turn off port 5900 (the VNC port).

The weird thing is that this worked fine on Tiger.

So perhaps with Snow Leopard this won’t be a problem… ?