Posted by Matt Osentoski
Wed, 24 Jan 2007 19:58:00 GMT
Hibernate has a class called Criteria that's used to help eliminate the logic in dynamic queries. I was building a Ruby on Rails application recently and wanted similar functionality, but didn't see the equivalent of a Criteria object in ActiveRecord. I searched around for a 3rd party library and found a few, but wasn't interested in a heavy API. So, I rolled my own Class to create dynamic WHERE clauses.
The class is called SqlBuilder and works on the principle that the ActiveRecord find() method can accept a String and Hash of parameters in the :conditions clause. What my class does is create the SQL string, inserting symbols in place of values.
To use my class in your own code, grab the sql_builder.rb file
here and follow the sample code below:
require 'sql_builder'
builder = SqlBuilder.new
builder.where_helper('division', params['division'], 'divisions.id')
builder.where_helper('vehicle', params['vehicles'], 'vehicles.id')
builder.where_helper('notes', params['notes'].strip, 'claims.notes', 'AND', 'LIKE')
sql_conditions, sql_conditions_hash = builder.get_sql_conditions()
@claims = Claim.find(:all, :include => [:divisions, :vehicles],
:conditions => [sql_conditions, sql_conditions_hash])
Disclaimer:
This is a very, very simple Class and probably won't work for every scenerio. If you have very complex needs you should check out some of the other libraries, that are out there.
Posted in Rails | Tags ActiveRecord, Rails | no comments
Posted by Matt Osentoski
Tue, 02 Jan 2007 19:42:00 GMT
Site5 has an automated process for adding Typo blogs to your website. This works fine except the blog is created in a directory off of the domain. For example: www.test.com/blog instead of www.test.com. Follow the steps below to install Typo into the root of your domain. This process should also work for your own custom Rails apps.
1. Setup SSH access on your Site5 account. Contact: shared@site5.com.
2. Download the latest version of Typo at: http://rubyforge.org/projects/typo/
3. Create a directory for your Rails applications. SSH into your account and create a
rails_apps directory.
> cd ~
> mkdir rails_apps
4. SFTP your Typo file into the rails_apps directory you just created. The
directory should be something like: /home/your_site_name/rails_apps
5. Expand the Typo file. SSH back into your account and issue the following
commands:
> cd rails_apps
> gunzip -c typo-4.0.3.tar | tar xvf -
(NOTE: replace typo-4.0.3.tar with the name of the file you grabbed from
rubyforge.org.)
6. Rename the original public_html directory.
> cd ~
> mv public_html public_html_orig
7. Create a symbolic link to typo's public directory.
> cd ~
> ln -s /home/your_site_name/rails_apps/typo-4.0.3/public public_html
8. Create the Typo database and database user.
Login to the SiteAdmin for your account and create a new database user and database.
Make sure you give the user the appropriate rights to the database you just created.
9. Prepopulate the typo database. (NOTE: This can also be accomplished using
the import function in phpMyAdmin from SiteAdmin.)
> mysql -u sitename_username -p
use your_database_name
source /home/your_site_name/rails_apps/typo-4.0.3/db/schema.mysql.sql
10. Modify the environment.rb file. This file should be located at:
/home/your_site_name/rails_apps/typo-4.0.3/config/environment.rb
Uncomment the following line:
ENV['RAILS_ENV'] = 'production'
Then add the RAILS_ROOT after:
RAILS_ROOT = '/home/your_site_name/rails_apps/typo-4.0.3'
11. Modify the database.yml file. This file should be located at:
/home/your_site_name/rails_apps/typo-4.0.3/config/database.yml
Modify the entries for the production application using the database information
from Step 8.
12. Verify your .htaccess file. Located at: /home/your_site_name/rails_apps/typo-4.0.3/public/.htaccess
This file should contain the following lines:
RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
13. Restart dispatch.fcgi. From a command prompt, run the following line:
> pkill -9 -u your_username_here -f dispatch.fcgi
(NOTE: replace your_username_here with the username you used to SSH into
the server.)
You should be all set! Try to access your domain, you should be greeted with the
Typo setup screen.
Posted in Rails | Tags Rails, Site5, Typo | no comments