Active Model in Rails

The technique we used was quite a hack as this is something that ActiveRecord wasn’t designed to do but now in Rails 3.0 we have a new feature called ActiveModel which makes doing something like this a lot easier.

Active Model in Rails

Before we get into the details of ActiveModel we’ll first describe the part of the application that we’re going to modify to use it.

Active Model in Rails 3.0
Active Model in Rails 3.0

The screenshot above shows a contact form that has been created using Rails’ scaffolding. The application has a model called Message that is currently backed by ActiveRecord which means that we’re managing messages through the database. We’re going to change the way this form works so that it just sends emails and doesn’t store messages in a database table.

When you’re thinking of doing something like this it’s always a good idea to first consider your requirements and make sure that you really don’t want to store the data from the form in a database as there are often good side-effects to doing this. A database can act as a backup and also makes it easier to move the message-sending into a queue in a background process. For the purposes of this example, however, we don’t want any of that functionality so we’re free to go ahead and make our model tableless.

The code for the Message class looks like this:

File name: /app/models/message.rb


class Message < ActiveRecord::Base
 validates_presence_of :name
 validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
 validates_length_of :content, :maximum => 500
end

Message inherits from ActiveRecord::Base as you would expect a model class to, but as we don’t want this model to have a database back-end we’re going to remove that inheritance. As soon as we do this, though, our form will no longer work as the validators are provided by ActiveRecord. Fortunately, we can restore this functionality by using ActiveModel.

If we take a look at the Rails 3 source code we’ll see the that there are activerecord and activemodel directories. The core Rails team has taken everything from ActiveRecord that wasn’t specific to the database backend and moved it out into ActiveModel. ActiveRecord still relies heavily on ActiveModel for the functionality that isn’t specific to the database and as ActiveModel is full-featured and thoroughly tested it’s great for use outside ActiveRecord.

It we take a look in the directory that contains the code for ActiveModel we can see the functionality that it provides.

We can see from the list above that ActiveModel includes code to handle callbacks, dirty tracking, serialization and validation, among other things. The last of these is exactly what we’re looking for.

The code for validations has the following comment near the top and we can see from it that it’s fairly easy to add validations to a model. All we need to do is include the Validations module and provide getter methods for the attributes that we’re calling validators on.

Now that we know this we can apply it to our Message model.

File name: /app/models/message.rb


class Message
 include ActiveModel::Validations

 attr_accessor :name, :email, :content

 validates_presence_of :name
 validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
 validates_length_of :content, :maximum => 500
end

Message inherits from ActiveRecord::Base as you would expect a model class to, but as we don’t want this model to have a database back-end we’re going to remove that inheritance. As soon as we do this, though, our form will no longer work as the validators are provided by ActiveRecord. Fortunately, we can restore this functionality by using ActiveModel.

If we take a look at the Rails 3 source code we’ll see the that there are activerecord and activemodel directories. The core Rails team has taken everything from ActiveRecord that wasn’t specific to the database backend and moved it out into ActiveModel. ActiveRecord still relies heavily on ActiveModel for the functionality that isn’t specific to the database and as ActiveModel is full-featured and thoroughly tested it’s great for use outside ActiveRecord.

It we take a look in the directory that contains the code for ActiveModel we can see the functionality that it provides.

We can see from the list above that ActiveModel includes code to handle callbacks, dirty tracking, serialization and validation, among other things. The last of these is exactly what we’re looking for.

The code for validations has the following comment near the top and we can see from it that it’s fairly easy to add validations to a model. All we need to do is include the Validations module and provide getter methods for the attributes that we’re calling validators on.

Now that we know this we can apply it to our Message model.

File name: /app/models/message.rb


class Message
 include ActiveModel::Validations

 attr_accessor :name, :email, :content

 validates_presence_of :name
 validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
 validates_length_of :content, :maximum => 500
end

This isn’t enough to get our model to behave as the controller expects it to, though. There are two problems in the create method. Firstly the call to Message.new won’t work as our Message model no longer has an initializer that takes a hash of attributes as an argument. Secondly, save won’t work as we don’t have a database backend to save the new message to.

Filename : /apps/controllers/messages_controller.rb


class MessagesController < ApplicationController
 def new
 @message = Message.new
 end

def create
 @message = Message.new(params[:message])
 if @message.save
 # TODO send message here
 flash[:notice] = "Message sent! Thank you for contacting us."
 redirect_to root_url
 else
 render :action => 'new'
 end
 end
end

We’ll fix the second of these problems first. While we can’t save a message we can check that it is valid, so we’ll replace @message.save with @message.valid?.

File name :/app/controllers/messages_controllers.rb


def create
 @message = Message.new(params[:message])
 if @message.valid?
 # TODO send message here
 flash[:notice] = "Message sent! Thank you for contacting us."
 redirect_to root_url
 else
 render :action => 'new'
 end
end

We can solve the first problem by writing an initialize method in the Message model that takes a hash as a parameter. This method will loop through each item in the hash and assign the value to the appropriate attribute for the message using the send method.

File name: /app/models/message.rb


class Message
 include ActiveModel::Validations

attr_accessor :name, :email, :content
 validates_presence_of :name
 validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
 validates_length_of :content, :maximum => 500
 def initialize(attributes = {})
 attributes.each do |name, value|
 send("#{name}=", value)
 end
 end
end

If we reload the form now we’ll see that we’re not quite there yet, however.

This time the error is caused by a missing to_key method in the Message model. The error is thrown by the form_for method so it seems that Rails itself is expecting our model to have functionality that it doesn’t yet support. Let’s add that functionality now.

Rather than guessing everything that Rails expects the model to have there’s a nice lint test included with ActiveModel that allows us to check whether our custom model behaves as Rails expects it to. If we include the ActiveModel::Lint::Tests module in a tests for the model it will check that the model has all of the required functionality.

The source code for the Lint::Tests module shows the methods that the model needs to respond to in order for it to work as it should, including to_key. We can make our model work by including a couple of ActiveRecord modules. The first of these is Conversion, which provides that to_key method and several others. The other module is the Naming module, but in this case we extend it in our class rather than including it as it includes some class methods.

As well as including the Conversion module we need to define a persisted? method in our model, which needs to return false as our model isn’t persisted to a database. With these changes in place our Message model now looks like this:

File name: /app/models/message.rb


class Message
 include ActiveModel::Validations
 include ActiveModel::Conversion
 extend ActiveModel::Naming
 attr_accessor :name, :email, :content
 validates_presence_of :name
 validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
 validates_length_of :content, :maximum => 500
 def initialize(attributes = {})
 attributes.each do |name, value|
 send("#{name}=", value)
 end
 end
 def persisted?
 false
 end
end

If we reload the form now it will work again which means that the Message model now satisfies all of the requirements that Rails 3 relies on for a model. If we try to submit the form we’ll see that the validators are working, too.

We’ve only covered a little of what ActiveModel provides in this episode but this should have been enough to whet your appetite and give you a reason to look more deeply into its source code to see what it can do. The code is well documented and structured so if you see something you might find useful then there should be enough information in the comments for that file to get you started using it.

deploy rails website with passenger -rails deployment

While working with PHP, I realized that deployment of a PHP application is far more easier as compared to deployment of a Ruby on Rails application. So I was wondering if there could be a way to deploy a Ruby on Rails application in a similar fashion, as easy as you check out the Rails project in a directory on the server, configure a VirtualHost in the webserver, restart the webserver and there you go, everything should work as expected.

deploy rails website with passenger -rails deployment with passenger
deploy rails website with passenger -rails deployment with passenger

Luckily I did find a saviour,

“Phusion Passenger” a.k.a “mod_rails” or “mod_rack”

Here I am putting down steps to ride Rails on Passenger.

Install Passenger

Passenger is available as a Rubygem. As root,

gem install passenger

Install Passenger module for Apache

passenger-install-apache2-module

During installation it will prompt:

The Apache2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

LoadModule passenger_module /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.11/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.11
PassengerRuby /usr/bin/ruby

NOTE: Do NOT copy-paste from here. Make sure you add the lines that passenger prompts you with.
It is specific to the OS installation. Mine was a Fedora Core 10 64-bit installation.

Next, it will help you to configure a VirtualHost for Apache

<VirtualHost *:80>
ServerName www.yourhost.com
DocumentRoot /somewhere/public

<Directory /somewhere/public>
AllowOverride all
Options -MultiViews
</Directory>

</VirtualHost>

Restart Apache:

As root,

/sbin/service httpd restart

That’s it, we are done.
Point your browser to your domain, and you should be able to see your Ruby on Rails Application.

Do keep in mind that the default Rails environment while using Passenger is “production”
To access your application in “development” or any other environment add the following line in your VirtualHost configuration:

RailsEnv development

Restarting your application deployed in production environment:

To restart your Rails application deployed in production environment you can restart Apache.
But that’s not a good solution if you are hosting multiple applications on the same server, or even generally, it doesn’t sound like a good idea to restart Apache every now and then.
To overcome that, you can restart your application in the following way:

From Rails Root Directory,

touch tmp/restart.txt

That’s it, this will restart you Rails application.

The best part is, Passenger is compatible with all the Rubygems and Rails plugins.

So nothing should break. I personally tried running a complex Rails application that uses Solr as the search engine along with acts_as_solr plugin, backgroundRB server for background processes, attachment_fu for file uploads etc, etc.

Everything worked like a charm.

For further configuration, please refer to the Passenger documentation found here:

You can also use Passenger to serve Non-Rails Ruby web applications. That can be a good alternative to the already prevalent mod_ruby module for Apache.
If you are using Nginx as your webserver, you can use Passenger module for Nginx.

There’s just one disadvantage to Passenger:

It doesn’t work on Windows. In any case, I personally never prefer to develop Rails (or any other) applications on Windows 🙂

Good news for Mac users:

fingertips has come up with an OS X preference pane for Phusion Passenger (a.k.a. mod_rails) that makes it a cinch to deploy Rails applications using Passenger on the Mac. It can be as simple as dragging a Rails application folder onto the preference pane! This is absolutely ideal for quick and easy Rails development on OS X.

You can download the Passenger Preference Pane here

References:

http://www.modrails.com/documentation/Users%20guide%20Apache.html

Ruby performance improvement of 63% in 1.9.1

Ruby is very powerful object-oriented language. new Ruby performance improvement. Working with pure Ruby is really fun and interesting to me. I am the real fan of Ruby language.

Ruby performance improvement

I am working Ruby on Rails for around last four years. There is always I am thinking and all ROR developers are thinking about performance of application.

We always compare Ruby on Rails with PHP, Java, Python, dot net. We came to know that where we are lacking in ROR. Just one issue performance and memory issue.

When we got news about Ruby 1.9 is comming with performance improvement of 63%. Yes, It is not a joke. This is really great news for every ROR developers. Ruby 1.9 and Rails 3.0 is the future of internet.

Now I will talk about Ruby Software engineer who developed and worked on the Ruby 1.9.

Masahiro Kanai, who improved the performance of several methods in Ruby 1.9. He is the age of high school, just 17.

This year, Ruby 1.9 by the Fibonacci sequence of operations (multiple-precision addition) is,
Ruby 1.8 is slower than I realized Mr. Kanai is a challenge to freedom of choice camp Ruby faster.

In front of all the participants had a similar problem occurring, Ruby of type String, Array types (structures that may have an embedded data structure type object itself) and some of the faster method, attention collection. Oden’s is known to be well-covered tongue teachers. Ruby continued even after the camp’s efforts to speed up to approximately some methods to remove the macro in a constant loop of 63 percent to about 8 percent of success in speeding. This patch has been adopted by the community of developers Ruby, Ruby has been incorporated into the trunk (October 5, 2009).

many professional engineers and developers, with patience to hold out that the problem is resolved, probably as a programmer in one or two talents.

Ketai that builds skills and career tips for the professional engineer. Interviewers are already familiar with this series, the Mr. Ikuo Takeuchi, Professor Department of Creative Informatics, Graduate School of Information Science and Engineering, University of Tokyo. This time, who undertook the voluntary and discover if training for a talented programmer.

His mentor was Koichi Sasada (ko1). The performances of the methods he worked have been bumped up 63% in maximum, 8% in average. His patches were applied to Ruby trunk in Oct. 5 this year.

What he did for Ruby Performance tuning?

He took unnecessary macro references out from a loop. Masahiro spotted macros below in array.c, string.c, and struct.c were referred every time Ruby checked whether data was hold in a structure or not. Even though data were constants, Ruby saw the macros to judge data’s presence in every loop.

What I say now more… I am really happy and I can say this is the biggest news of 2010 in IT world.

One thing I want to mention here about Ruby on Rails. This thing is in mind of all ROR developers.

“Thank God, No need to look Jruby”

He took unnecessary macro references out from a loop. Masahiro spotted macros below in array.c, string.c, and struct.c were referred every time Ruby checked whether data was hold in a structure or not. Even though data were constants, Ruby saw the macros to judge data’s presence in every loop.

Ruby on Rails 3.0 released with new features

I am working on Ruby on Rails for past three and half year. It is really great to working with Ruby on Rails. Because working on Ruby on Rails is a really great feeling daily inventing new things, trying new things and contributing to ROR community. Discussing new things with ROR developers is really great.

Ruby on Rails 3.0 released with new features
Ruby on Rails 3.0 released with new features

On 5th Feb we got news about release of Rails 3.0 beta. We all developers are really waiting for that day. We checked the new version of Rails but what is more interesting.

The interesting part is what is new in Rails 3.0. So many developers contributed in Rails 3.0. They really worked and planned so hard.

Here I am making list what Rails 3.0  has, means new features:

  • Brand new router with an emphasis on RESTful declarations
  • New Action Mailer API modelled after Action Controller (now without the agonizing pain of sending multipart messages!)
  • New Active Record chainable query language built on top of relational algebra
  • Unobtrusive JavaScript helpers with drivers for Prototype, jQuery, and more coming (end of inline JS)
  • Explicit dependency management with Bundler

If you want to use the Rails 3.0 then you should install first following gems.

#gem install tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n
#gem install rails --pre

Or you can use following command to install whole Rails 3.0 with dependencies

gem uninstall rubygems-update
gem update --system
#gem install rails3b

Important Note: Rails 3 requires Ruby 1.8.7+ , you should install latest Ruby version. You can download latest Ruby version from here.

Ruby 1.9.1

Here I am giving the list of gems and plugins which are compilable of Rails 3.0. you should check the following URL:

http://wiki.rubyonrails.org/rails/version3/plugins_and_gems

Now I am started working on Rails 3.0 and I am searching for the issues with Rails 3.0 because Rails 3.0 is in Beta so all ROR developer need to come up with issues of Rails 3.0 and try to fix that or submit that issue to community.

How to host multiple rails site on Nginx

Nginx famous now. We given information for how to host multiple rails site on Nginx. we given the configuration code along with their detailed information.

How to host multiple rails site on Nginx

User following code in Nginx.conf file.. and paste into that file.
#vim /etc/nginx/nginx.conf


http {
include       /etc/nginx/mime.types;
default_type  application/octet-stream;
access_log  /var/log/nginx/access.log  main;
sendfile        on;

upstream mongrel_cluster_example1 {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
}

upstream mongrel_cluster_example2 {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}

# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;

server {
listen       80;
server_name  example1.com example2.net;
client_max_body_size 120M;

set $myroot /var/www/html;
if ($host ~* example1\.com$) {
set $myroot /home/example1/public;
}

if ($host ~* example2\.net$) {
set $myroot /home/example2/public;
}

root $myroot;

location ~* ^/(images|stylesheets|javascripts).+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
{
root $myroot;
}

location / {

if ($host ~* example1\.com$) {
proxy_pass      http://mongrel_cluster_example1;
} #if check for domain qa.teenangel ends here

if ($host ~* example2\.net$) {
root /home/rail_project/myproject/public;
proxy_pass      http://mongrel_cluster_example2;

} #if check for domain ends here

}

}

}

USA State list for Rails

Every time we need this migration script for our projects
Use following command for Model generate
[siwan@localhost siwan]$ ruby script/generate model UsStates
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/us_states.rb
create test/unit/us_states_test.rb
create test/fixtures/us_states.yml
exists db/migrate
create db/migrate/20091117115011_create_us_states.rb
[siwan@localhost siwan]$

Open the /db/migrate/20091117115011_create_us_states.rb file and paste following code:

class CreateUsStates ‘Alabama’, :abbreviation => ‘AL’
UsStates.create :name => ‘Alaska’, :abbreviation => ‘AK’
UsStates.create :name => ‘Arizona’, :abbreviation => ‘AZ’
UsStates.create :name => ‘Arkansas’, :abbreviation => ‘AR’
UsStates.create :name => ‘California’, :abbreviation => ‘CA’
UsStates.create :name => ‘Colorado’, :abbreviation => ‘CO’
UsStates.create :name => ‘Connecticut’, :abbreviation => ‘CT’
UsStates.create :name => ‘Delaware’, :abbreviation => ‘DE’
UsStates.create :name => ‘District of Columbia’, :abbreviation => ‘DC’
UsStates.create :name => ‘Florida’, :abbreviation => ‘FL’
UsStates.create :name => ‘Georgia’, :abbreviation => ‘GA’
UsStates.create :name => ‘Hawaii’, :abbreviation => ‘HI’
UsStates.create :name => ‘Idaho’, :abbreviation => ‘ID’
UsStates.create :name => ‘Illinois’, :abbreviation => ‘IL’
UsStates.create :name => ‘Indiana’, :abbreviation => ‘IN’
UsStates.create :name => ‘Iowa’, :abbreviation => ‘IA’
UsStates.create :name => ‘Kansas’, :abbreviation => ‘KS’
UsStates.create :name => ‘Kentucky’, :abbreviation => ‘KY’
UsStates.create :name => ‘Louisiana’, :abbreviation => ‘LA’
UsStates.create :name => ‘Maine’, :abbreviation => ‘ME’
UsStates.create :name => ‘Maryland’, :abbreviation => ‘MD’
UsStates.create :name => ‘Massachutsetts’, :abbreviation => ‘MA’
UsStates.create :name => ‘Michigan’, :abbreviation => ‘MI’
UsStates.create :name => ‘Minnesota’, :abbreviation => ‘MN’
UsStates.create :name => ‘Mississippi’, :abbreviation => ‘MS’
UsStates.create :name => ‘Missouri’, :abbreviation => ‘MO’
UsStates.create :name => ‘Montana’, :abbreviation => ‘MT’
UsStates.create :name => ‘Nebraska’, :abbreviation => ‘NE’
UsStates.create :name => ‘Nevada’, :abbreviation => ‘NV’
UsStates.create :name => ‘New Hampshire’, :abbreviation => ‘NH’
UsStates.create :name => ‘New Jersey’, :abbreviation => ‘NJ’
UsStates.create :name => ‘New Mexico’, :abbreviation => ‘NM’
UsStates.create :name => ‘New York’, :abbreviation => ‘NY’
UsStates.create :name => ‘North Carolina’, :abbreviation => ‘NC’
UsStates.create :name => ‘North Dakota’, :abbreviation => ‘ND’
UsStates.create :name => ‘Ohio’, :abbreviation => ‘OH’
UsStates.create :name => ‘Oklahoma’, :abbreviation => ‘OK’
UsStates.create :name => ‘Oregon’, :abbreviation => ‘OR’
UsStates.create :name => ‘Pennsylvania’, :abbreviation => ‘PA’
UsStates.create :name => ‘Rhode Island’, :abbreviation => ‘RI’
UsStates.create :name => ‘South Carolina’, :abbreviation => ‘SC’
UsStates.create :name => ‘South Dakota’, :abbreviation => ‘SD’
UsStates.create :name => ‘Tennessee’, :abbreviation => ‘TN’
UsStates.create :name => ‘Texas’, :abbreviation => ‘TX’
UsStates.create :name => ‘Utah’, :abbreviation => ‘UT’
UsStates.create :name => ‘Vermont’, :abbreviation => ‘VT’
UsStates.create :name => ‘Virginia’, :abbreviation => ‘VA’
UsStates.create :name => ‘Washington’, :abbreviation => ‘WA’
UsStates.create :name => ‘West Virginia’, :abbreviation => ‘WV’
UsStates.create :name => ‘Wisconsin’, :abbreviation => ‘WI’
UsStates.create :name => ‘Wyoming’, :abbreviation => ‘WY’

end

def self.down
drop_table :us_states
end
end

Goodlunk, This code will save your problem of fetching USA state names and abbreviations as per id

Check my controller code…Just put in your controller

@us_states = USStates.find(:all)

<% form_for :customer, @customer, :url => { :action => “new_application” } do |f| %>
<%= f.select :state_id,  @us_states.collect {|state| [ state.abbreviation, state.id ] } %>
<% end %>

Nginx Rule for Rails Application

Use following code for setting up Nginx for rails.

http {
include       /etc/nginx/mime.types;
default_type  application/octet-stream;</code>

log_format  main  '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

# Timeouts
client_body_timeout   5;
client_header_timeout 5;
keepalive_timeout     55;
send_timeout          5;

upstream mongrel_cluster {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}
server {
listen       80;
server_name  localhost.localdomain;
#charset koi8-r;
#access_log  logs/host.access.log  main;

#for images routing
location ~* ^/(images|stylesheets|javascripts).+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
{
root /var/www/html/Projects/Project_Name/public;
}
# concurs with nginx's one

location  / {
proxy_pass	http://mongrel_cluster;
#Fix for host name and redirection to domain

}

}

<code>

Install Apatana Studio on Fedora 9

Installation of Apatana in Detail

1. Download zip file from this location(http://www.aptana.com/studio/download)
File name:Aptana_Studio_Setup_Linux_1.2.7.zip
2. Then uncompress the .zip file. and copy the resulting ‘aptana’ folder under ‘/home/siwan’.(siwan is my username for fedora, you have replace your username)
3. Install xulrunner (for install xulrunner user following command, Became root first)
yum install xulrunner
or follow this step
Down load xulrunner from this url
http://releases.mozilla.org/pub/mozilla.org/xulrunner/releases/1.8.1.3/contrib/linux-i686/xulrunner-1.8.1.3.en-US.linux-i686-20080128.tar.gz
Unarchive using file roller to get a ‘xulrunner’ folder.
Open a terminal, and type the following commands, changing the path in the last line ie nigel to your username.
(for install xulrunner user following command, Became root first)

    mkdir /usr/lib/xulrunner
    cp -r /home/nigel/Download/xulrunner/* /usr/lib/xulrunner

4. Create a new file called say ‘aptana.sh’ and make it executable
use this command for make file executable.
Became root user first.
cd /home/siwan/aptana/
chmod +x aptana.sh
5. Copy the following into that text file

#!/bin/sh
MOZILLA_FIVE_HOME=/usr/lib/xulrunner
if [ $LD_LIBRARY_PATH ]; then
LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME:$LD_LIBRARY_PATH
else
LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME
fi
export MOZILLA_FIVE_HOME LD_LIBRARY_PATH
/home/siwan/aptana/AptanaStudio

Then run the aptana.sh file.
Enjoy with Aptana Studio

Memcached Server with Rails

To install memcached server on linux/fedora box
yum install memcached

Find help for memcached commands and option
memcached -help

Start memcached server(11211 is default port Number for memcached server)
memcached -m 500 -l 192.168.2.4 -p 11211 -vv
(192.168.2.4 this is my local ip address you can cahnge that)

Stop the Memcached server
ps -ef|grep memcached
kill PROCESS_ID

start memcached server with
memcached -m 500 -l 192.168.2.4 -p 11211 -vv

How to install on Windows server:
1.  Downloaded this Win32 port of memcached by Kenneth Dalgleish.(download from following url)
http://thewebfellas.com/blog/2008/6/9/rails-2-1-now-with-better-integrated-caching
2. Extracted the files to C:\Program Files\memcached
3. Opened a command prompt in the folder and installed memcached as a service using memcached -d install
4. Started the service using net start “memcached Server”

How to use memcache in Rails app?
Open  /config/environments/development.rb for development mode,
production.rb for production mode.
paste following line into file.
config.cache_store = :mem_cache_store, ‘127.0.0.1’, ‘127.0.0.1:11211’ , { :namespace => ‘dev’ }
(127.0.0.1 this is my local ip address you can cahnge or point out to any server where memcached server is running)
Restart mongrel server. you are ready to use memcached server

How to use in Rails
@user= User.find(1)—–OLD Code

New Code With Caching:
@user = cache([‘User’,1],:expires_in => 30.minutes) do
User.find(1)
end

deconstructing date_select in rails

// Reconstruct a date object from date_select helper form params

// place this code into your application.rb file
def build_date_from_params(field_name, params)
Date.new(params["#{field_name.to_s}(1i)"].to_i,
params["#{field_name.to_s}(2i)"].to_i,
params["#{field_name.to_s}(3i)"].to_i)
end

//You can changes order of displaying into view
<%= date_select ‘from’, ‘date’ ,:order => [:day, :month, :year] %>
<%= date_select ‘to’, ‘date’,:order => [:day, :month, :year]  %>

//goes into controller -- add your own error handling/defaults, please!
@from = build_date_from_params(:date, params[:from])
@to = build_date_from_params(:date, params[:to])

Comparing date time into controller

@users_reports = User.find(:all, :conditions=>[‘created_at >= ? and created_at < ?’, @to, @from])