Monday, September 13, 2010

access SQLServer 2008 with Rails 3 from Ubuntu Lucid Lynx 10.04

This tutorial is meant to be used with rails 3 final version, ruby 1.9.2 final version too.

The purpose is not to install rails 3 or ruby 1.9.2, tutorials to do so are mentioned in my 2 earlier posts of August and September 2010. The idea here is to connect from an Linux Ubuntu 10.04 station (or server) where you have your rails application to a Windows station (or server) where your SQL Server 2008 database resides.

First, to connect your machine, you need to be able to perform a connection to your database. To do so, you'll have to install FREETDS and UNIX-ODBC.

1. Install FREETDS

sudo apt-get install freetds

2. Configure FREETDS

edit the file /usr/local/etc/freetds.conf

insert the following text:

[TDS]
host = NAMEOFWINDOWSHOST (or an IP address like 192.168.0.130 if you are not on a domain)
port = 1433
tds version = 8.0

3. Test FREETDS

tsql -S 192.168.0.130 -p 1433 -U username -P password

You should see a prompt if it works. Then you can go ahead and parameterize UNIX-ODBC

4. Install UNIX-ODBC

sudo apt-get install unix-odbc

5. Configure your ODBC

edit the file /etc/odbc.ini

insert the following text:

[192.168.0.130]
Driver = FreeTDS
Description = ODBC Connection via FreeTDS
Trace = No
Servername = 192.168.0.130
Database = MySQLDatabaseName


edit the file /etc/odbcinst.ini

insert the following text:

[FreeTDS]
Description = TDS Driver (Sybase/MS SQL)
Driver = /usr/lib/odbc/libtdsodbc.so
Setup = /usr/lib/odbc/libtdsS.so
CPTimeout = 
CPReuse = 
UsageCount = 1


6. Test UNIX-ODBC

isql 192.168.0.130 username password

The ip address here can be replaced by the name you have on top of /etc/odbc.ini (it's the name of your ODBC). (if you have a desktop version, you can also install ODBCConfig to edit the ODBC connection).

if it went correctly, you should be able to query your database with SQL (Warning, the output will look a bit messy but it's just a connection test after all).

The second part is to verify that the firewall on your network or machine allows a connection to your SQL Server. Usually you have to specify a rule in your firewall to allow the Unix machine to 'talk' to the Windows machine using the port 1433 (or any other port if you changed the default port for TCP communications in your SQL Server configuration).

The third part is entering (at last) the ruby and rails world. You will need to install RUBY-ODBC (at least version 0.99992) and the ACTIVERECORD-SQLSERVER-ADAPTER (at least version 3.0.0)

The current version that comes when you do GEM INSTALL 'RUBY-ODBC' does not work with Ruby 1.9.2 because the STR2CSTR has been deprecated in Ruby 1.9.2. So you'll have to install this manually (details can be found in this thread)

download the fixed version at http://www.ch-werner.de/rubyodbc/ruby-odbc-0.99992pre3.tar.gz

uncompress it in 'ruby-odbc-0.99992pre3', go to the directory and install it using the following commands:

ruby -Cext extconf.rb
make -C ext
make -C ext install


Note: more details are available in the README file in the same directory.

Then install the activerecord-SQLServer-adapter

gem install activerecord-sqlserver-adapter

(or add it to your Gemfile).

Then edit your application and modify the config/database,yml file as follows:

development:
  adapter: sqlserver
  mode: odbc
  dsn: 192.168.xxx.xxx # This is your DSN name (here the IP is the DSN name too).
  username: testdummyusername
  password: testdummmypassword
  host: ILOVEMYWINDOWSPCNAME #not necessary

That's it. Now you can test your application to see if you can connect.

RAILS SERVER

Sunday, August 22, 2010

Ruby 1.9.2 for windows is released

Ruby 1.9.2 for windows is released, You can find the binaries here. You will also find all instructions required for installation.

An example of setup with rails 3 was documented on this blog.

Saturday, August 21, 2010

RVM issues while setting up Ruby 1.9.2 and Rails 3 rc2 on Lucid Lynx

In order to test rails 3 and ruby 1.9.2, I've migrated to Ubuntu 10.04. During the installation I ran into a few issues and had conflicts with both my version of OS and my versions of ruby.

Here is the list of the lessons learned :

Rule number 1 : always install the latest version of RVM (it's updated very frequently). gem install rvm

Rule number 2 : Go and modify your bash files when it tells you to do so (I did not do it at first and it completely messed up my later installs, with RVM mixing old and new paths for gems).

Rule number 3 : On lucid lynx (Ubuntu 10.04), you cannot update the rubygems by doing gem update -system. It's likely that you will have version 1.3.5 as it is the official version supported by the distribution. To upgrade to the required 1.3.7, follow this procedure :

sudo gem install rubygems-update
sudo rubygems_update


when you do gem --version
you should now see > 1.3.7 instead of > 1.3.5

if you still see 1.3.5, i suggest you to do the following before the above procedure:

sudo apt-get remove rubygems

Rule number 4: install the pre-requisites for Rails 3 rc2

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


Rule number 5: install rails

gem install rails --pre

VoilĂ , you can now start to play or upgrade your apps (see the screencasts below).

As soon as Luis Lavena releases the new 1.9.2 ruby installer I will try and document the same process on windows (with PIK, the windows equivalent of RVM).

There are a lot of great Rails 3 tutorials available now. If you start I recommend the railscasts numerous episodes on the topic or the envylabs screencasts made for the latest railsconf. They both do a fantastic job for the community.

For the help with the install, the credits go to the people in this thread.

Thursday, August 19, 2010

Thursday, August 12, 2010

How to develop live search textbox in Ruby on Rails

A nice tutorial for a live search textbox using jquery can be found here

Saturday, June 12, 2010

Using jquery flexselect (a predictive select control) in your rails views

This tutorial will show how to use Jquery flexselect with rails. It is the first part of a series of tutorials on rails UI showing how to enhance the basic rails view in an unobstrusive way.

Flexselect replaces a select control with a textbox control where you can type anything. While typing, flexselect will instantly show you a list of corresponding matches within the select list. The main advantage of this control is to allow the instant search in a long list of options (replacing the painfull user-experience which leads to display annoyances with a normal select control). You can type characters in any order and flexselect will still show you a match. If the user has javascript disabled, the normal select control will render and your page will still work.

For the tutorial, a small application will be build from scratch :

1. In the console, type

rails flexselectdemo 

You will notice the usage of the default sqlite3 database (you can change database.yml in /config to use any other database adapter).

2. create a basic scaffold for a post

ruby script/generate scaffold post name:string author:string 

3. Edit the view to add the jquery framework and flexselect stylesheets in the head section

<%= stylesheet_link_tag 'scaffold' %>
  <%= stylesheet_link_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/redmond/jquery-ui.css", :media => "screen", :cache =>  true  %>
  <%= stylesheet_link_tag 'flexselect', :media => "screen", :cache =>  true %>


4. Edit the view to add the jquery framework and flexselect javascripts at the end of your body section

<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js",:cache => true %>
    <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js",:cache => true %>
    <%= javascript_include_tag  "application", :cache => true %>
    <%= javascript_include_tag  "flexselect/liquidmetal", :cache => true %>
    <%= javascript_include_tag  "flexselect/jquery.flexselect", :cache => true %>

5. Copy the files from the flexselect plugin to the relevant public directories:

the jquery.flexselect.js and liquidmetal.js files go to your public/javascripts/flexselect/ directory

the flexselect.css file goes to your public/stylesheets directory

6. Edit the post edit view:

Replace

<%= f.label :author %>

    <%= f.text_field :author %>
  

with


<select class="flexselect">
                 <option value="1">George Washington</option>
                 <option value="2">John Adams</option>
                  <option value="3">Thomas Jefferson</option>
                  <option value="4">Bill Gates</option>
                  <option value="5">Nicolas Sarkozy</option>
                  <option value="6">Gordon Brown</option>
                  <option value="7">Angela Merkel</option>
                  <option value="8">Moritz Leuenberger</option>
                  <option value="9">Barrack Obama</option>
            </select>

This will give you



for the select box and 


for the flexselect box

7. Add the following to application.js in the public/javascripts folder

// Jquery : flexselect plugin
jQuery(document).ready(function() {
  $("select[class*=flexselect]").flexselect();
});


8. Create the database and execute the migration

rake db:create:all 
rake db:migrate 

9. Launch the server

ruby script/server 

When you edit a post, you should see a textbox instead of the select box. If you type inside, you will see immediately the available options in a list below.

This ends up this tutorial on the implementation of jquery flexselect with ruby on rails. Note that the tutorial was simplified compared to a real world implementation (ex. jquery is loaded from google repository and direclty in the view / flexselect is not yielded by the chosen model but is in the general view and so on).

You can find flexselect here.

Wednesday, June 9, 2010

Datamapper SQL Server adapter

In case you're looking for it, here is the Gem for the SQLServer datamapper adapter and here is the release note of the latest release (v1.0) of datamapper.

DataMapper In 20 min

Check out this SlideShare Presentation:

Replacing ActiveRecord With DataMapper

Check out this SlideShare Presentation:

Legacy Database with Datamapper

Oh yes, we do fight sometimes with our sqlserver legacy database. So anything that can ease the pain is welcomed.

Sunday, May 16, 2010

Why MongoDB is awesome

And we continue on the MongoDB serie with a presentation by the creator of MongoMapper for Ruby.

Saturday, May 15, 2010

MongoMapper - Mapping Ruby to and from Mongo

If you want an alternative database solution, more document oriented, check out this SlideShare Presentation:

Monday, May 3, 2010

Saturday, April 17, 2010

Thursday, February 18, 2010

Git 101 Presentation

Because everybody needs GIT in the rails world, check out this SlideShare Presentation:

Wednesday, February 10, 2010

ruby installer for Windows

A new version of the ruby installer for windows is available here

Saturday, February 6, 2010

Rails 3 beta has landed (and some dared install it on Windows !)

Check this tutorial if you want to install a fresh Rails 3 on Windows.

Remember to start using Ruby 1.9 as the old ruby 1.6 will be soon deprecated.

Note : at this point, a lot of gems and plugins are not adapted yet so you might want to wait before migrating existing apps and stick with rails 2.3.5 for your production apps. I recommend to start testing rails 3 right now (maybe in a virtual machine to avoid messing your actual environment).

Friday, February 5, 2010

Monday, February 1, 2010

Tuesday, January 19, 2010

Saturday, January 16, 2010

rails and heroku on windows - tutorial

A quick link to install rails and make it work with heroku on windows.

Original post is here