Posts Tagged ‘ruby’

Run acts_as_solr in JRuby in background mode

When you try and run rake solr:start or rake solr:stop it uses Kernel.fork to spawn of a child process. However in JRuby this is disabled by default due to concurrency issues. And this prevents your solr rake tasks from running under a JRuby environment…

The Problem

When you try and run rake solr:start or rake solr:stop it uses Kernel.fork to spawn of a child process.
However in JRuby this is disabled by default due to concurrency issues. There is an option to enabling fork (jruby -J-Djruby.fork.enabled=true)
within JRuby but it is experimental and as the warning says, “WARNING: fork is highly unlikely to be safe or stable on the JVM.” as it can cause all sorts of weird and wonderful side-effects.

The Solution

The solution therefore is to simply alter the solr:start and solr:stop tasks to use Kernel.exec instead.
To do this find the solr rake tasks usually in {RAILS_ROOT}/vendor/acts_as_solr/lib/tasks/solr.rake. In the start task all that is required is to comment out the start of the fork block. so you only have the exec method call,
This will not start the solr process in the background. To do so you just have to add “&” at the end of the exec command, which causes the jar file to run in background mode.

Ruby Mixin Tutorial

In Java you just have classes (both abstract and concrete) and interfaces. The Ruby language provides classes, modules, and a mix of both. In this post I want to dive into mixins in Ruby.
In the Ruby language a mixin is a class that is mixed with a module. In other words the [...]

Paginating multiple models using will_paginate on the same page

The will_paginate plugin makes pagination for your models in Ruby on Rails ridiculously simple. However sometimes you’ll find yourself wanting to paginate over two or more models on a single page. For instance, you might want to display a list of users and administrators on a single page along with a pager for each model [...]

List of useful rake tasks for Rails…

rake cache:clear
# Clears all cached pages
rake db:bootstrap
# Loads a schema.rb file into the database and then loads the initial database fixtures.
rake db:bootstrap:copy_default_theme
# Copy default theme to site theme
rake db:migrate
# Migrate the database through scripts in db/migrate. Target specific version with VERSION=x
rake db:schema:dump
# Create a db/schema.rb file that can be portably used against any DB supported [...]

Setup Capistrano to deploy Rails application on Amazon EC2 with Git

1: Create a new Rails app – we’ll call is ‘deploytest’
$ rails deploytest
$ cd deploytest
2: Create a local Git repository for it
$ git init
$ git add *
$ git commit -a -m ‘initial commit’
$ git status
3: Create a couple of Capistrano files
$ capify .
4: Edit config/deploy.rb
# The name of your app
set :application, “deploytest”
# The directory on [...]

Send SMS from Ruby On Rails application using web service, SOAP API

SOAP4R is a Ruby library for accessing Web Services via SOAP. Recently I had a chance to explore SOAP4R. Here’s how to get started with it.

Installation
Although Ruby 1.8.x comes with SOAP4R in its standard library, it is an old, buggy version. I highly recommend using the latest gem (1.5.8 as of the this update). It [...]

Get location from IP address in Ruby On Rails for free….

Find below the code for finding location from IP address using IP location tools.
require ‘net/http’
require ‘rexml/document’
include REXML

class MapsController < ApplicationController
def index
@location = locateIp()

end

def locateIp
ip = request.remote_ip
ips = ip.to_s
url = “http://iplocationtools.com/ip_query.php?ip=”+ips

xml_data = Net::HTTP.get_response(URI.parse(url)).body

xmldoc = REXML::Document.new(xml_data)

# Now get the root element
root [...]

Generating ZIP files via Ruby on Rails using rubyzip

gem install rubyzip
Then, in the model that I’m using to generate the zip bundles, I add a couple “require” statements:
require ‘zip/zip’
require ‘zip/zipfilesystem’

class Album < ActiveRecord::Base
(…)
end
Next, I added a class method called bundle, which when called will use rubygem to generate the zip file. Note: the “permalink” attributes of Album and Artist are populated [...]

New features in Rails 2

Today i was reading about the new features of Rails 2, there are a lot of changes, for an overview you can checkout the official rails blog announcement. Here is a little list of major changes and new features:

ActionMailer::Base.server_settings [...]

Ruby thumbnail generator

Ruby thumbnail generator is simple script which is ideal to use in your Ruby on Rails application to quickly generate thumbnails of any proportions. Just set width and height and get the image.
1. copy following code into /controllers/thumb_controller.rb
2. edit /config/routes.rb and add this line:map.connect “thumb/*specs”, :controller => “thumb”, :action => “index”
3. create directory /imagelib/ in [...]