Sunday, March 11, 2012

Rails 3.2 and Ruby 1.9.3 on Windows

RailsInstaller 2.1 for Ruby 1.9.3 and Rails 3.2 for Windows has been released! Go read about it at engineyard blog … and download it from railsinstaller.org !

It includes:


Wednesday, September 7, 2011

Rails installer 2.0 has been released

RailsInstaller 2.0.0 for Windows has been released! Go read about it at engineyard blog … and download it from railsinstaller.org !

Sunday, September 4, 2011

Duplicate a model in rails 3.1

Ever wanted to duplicate a model in rails ?

This blog post will answer the following issue common to almost all gui. In order to facilitate the life of the administrator, we often see the possibility to duplicate an existing record instead of creating a new one from scratch.

Before rails 3.1, this action was performed using the active-record clone method.

Rails 3.1 is introducing the dup method. Just by it's name, it seems to be the perfect candidate for our task.

How to use it ?

First let's test in the console:

rails c  

a = Model.first # finds the first instance of the model "Model"  
b = a.dup # duplicates the record  
b.save # saves the record into the database.

If you look into your database, you can see that a new record has been created. It is identical to the first record excepted that it has a new id.

You can also modify one of the field of the model to show to the user that he is working with a duplicate and that he has to edit it. I usually prefix the name with "dup_" so I get "dup_Georges" indicating that I need to modify the record.

b.name = "dup_" + b.name

That's it. A simple but very useful method. In my opinion it's the 8th method that has been forgotten in the crud scaffold.

Some details about the implementation:

# ActiveRecord::Base#dup and ActiveRecord::Base#clone semantics have changed to closer match normal Ruby dup and clone semantics.

# Calling ActiveRecord::Base#clone will result in a shallow copy of the record, including copying the frozen state. No callbacks will be called.
# Calling ActiveRecord::Base#dup will duplicate the record, including calling after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return true for new_record?, have a nil id field, and is saveable.

Tuesday, April 19, 2011

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.