Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Thursday, January 15, 2015

Error installing rb-gsl gem on ubuntu

While installing rb-gsl gem on my gemset from ruby 2.1.0 got an error trailed like below

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /home/praveen/.rvm/rubies/ruby-2.1.5/bin/ruby -r ./siteconf20150115-4389-1qh8ghv.rb extconf.rb
extconf.rb:6: warning: Insecure world writable dir /opt/android-sdk in PATH, mode 040777
*** ERROR: missing required library to compile this module: No such file or directory - gsl-config
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/home/praveen/.rvm/rubies/ruby-2.1.5/bin/ruby
    --with-gsl-version

extconf failed, exit code 1

Gem files will remain installed in /home/praveen/.rvm/gems/ruby-2.1.5/gems/rb-gsl-1.16.0.2 for inspection.
Results logged to /home/praveen/.rvm/gems/ruby-2.1.5/extensions/x86_64-linux/2.1.0/rb-gsl-1.16.0.2/gem_make.out


there is one system dependency that is required to pursue further in ubuntu install libgsl0-dev package, install it like..
sudo apt-get install libgsl0-dev
That's it now retry installing rb-gsl gem.. it worked for me :)





Saturday, September 8, 2012

Rails behaviour of form_for serving from a custom ruby class, (rails2 and rails3 differentiates)

Hi,

It has been so long writing a post and for a change this time it not my laziness but had been really occupied from office front.

Anyhow, here is one strange thing that I found with "form_for" in Rails2 and Rails3.
In Rails2, if we have a class in ruby (not derived from Activerecord::Base) some thing like the one below..

class SomeRubyClass

  attr_accessor :file_name, :file_data, :class_name, :error

  def initialize(file_name, file_data, class_name, error)
    @file_name = file_name
    @file_data = file_data
    @class_name = class_name
    @error = error
  end

  def self.first
    #Some code, which will give an object
  end
end


We can use form_for with it something like this

class SomeController < ApplicationController
  def index
    @some_ruby_class = SomeRubyClass.new("file_name", "file_data", "class_name", "error")
  end

  def create
    @some_ruby_class = SomeRubyClass.first
    @some_ruby_class.class_name = params[:some_ruby_class][:class_name]
    @some_ruby_class.file_name = params[:some_ruby_class][:file_name]
  
    #some more code
    redirect_to :back
  end
end

<% form_for @some_ruby_class, :url => {:action => :create} do |f| %>
    <%= f.text_field :class_name %>
    <%= f.text_field :file_name %>
    <%= f.submit 'Update' %> 
<% end %>

and we can do manipulation as per the need.
But in Rails3 this thing is not supported with form_for
It gives exception like...
undefined method `model_name' for SomeRubyClass:Class' 
and to make it work we have to add these lines 
1. extend ActiveModel::Naming
2. add to_key instance method which must return an array

so added it and result is something like this
class SomeRubyClass
  extend ActiveModel::Naming

  attr_accessor :file_name, :file_data, :class_name, :error

  def initialize(file_name, file_data, class_name, error)
    @file_name = file_name
    @file_data = file_data
    @class_name = class_name
    @error = error
  end

  def self.first
    #Some code, which will give an object
  end

  def to_key
    ["some_key", "some_value"]
  end

end

and it works! Now question arises, Rails3 is imposing me to use ActiveModel (which was initially meant to provide ActiveRecord base type facilities like validation etc.) for simple things like form_for why?? am I missing something here.

Saturday, December 17, 2011

Issue: Installing ree ruby via rvm in ubuntu 11.10

So if you have tried installing REE on Ubuntu 11.10 something like
praveen@praveen-laptop:~$ rvm install ree
Installing Ruby Enterprise Edition from source to: /home/praveen/.rvm/rubies/ree-1.8.7-2011.03
ree-1.8.7-2011.03 - #fetching (ruby-enterprise-1.8.7-2011.03)
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 184 100 184 0 0 107 0 0:00:01 0:00:01 --:--:-- 567
100 7713k 100 7713k 0 0 32397 0 0:04:03 0:04:03 --:--:-- 33764
ree-1.8.7-2011.03 - #extracting ruby-enterprise-1.8.7-2011.03 to /home/praveen/.rvm/src/ree-1.8.7-2011.03
Applying patch 'tcmalloc' (located at /home/praveen/.rvm/patches/ree/1.8.7/tcmalloc.patch)
Applying patch 'stdout-rouge-fix' (located at /home/praveen/.rvm/patches/ree/1.8.7/stdout-rouge-fix.patch)
Applying patch 'no_sslv2' (located at /home/praveen/.rvm/patches/ree/1.8.7/no_sslv2.diff)
ree-1.8.7-2011.03 - #installing
ERROR: Error running './installer -a /home/praveen/.rvm/rubies/ree-1.8.7-2011.03 --dont-install-useful-gems ', please read /home/praveen/.rvm/log/ree-1.8.7-2011.03/install.log
ERROR: There has been an error while trying to run the ree installer. Halting the installation.

and now that you will follow the log you will notice some dependencies not met and hence installation failed, some thing like this.
Checking for required software...
* C compiler... found at /usr/bin/gcc
* C++ compiler... found at /usr/bin/g++
* The 'make' tool... found at /usr/bin/make
* The 'patch' tool... found at /usr/bin/patch
* Zlib development headers... not found
* OpenSSL development headers... not found
* GNU Readline development headers... not found
but as per suggestion
* To install GNU Readline development headers:
Please run apt-get install libreadline5-dev as root.
Package libreadline5-dev is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source.
So instead install libreadline-gplv2-dev
* To install GNU Readline development headers:
Please run apt-get install libreadline-gplv2-dev as root.
And you are good to go :)

These information are not tough to find but summarizing it just in case some body is in need of it in a quicky!

Thursday, November 17, 2011

What the hell! Can't serialize a Mysql object with Marshal in Ruby

Okey so got this strange information late in night at about 3.50am and it took more than 2 hrs for me to surrender. uff :D
and this is something that came in picture
`dump': no marshal_dump is defined for class Mysql (TypeError

and this is a small code snippet which will give you this situation
require "rubygems"
require "mysql"

class RandomClass
def initialize
@db_instance = Mysql.real_connect("hostname", "username", "password", "database_name")
end
end

random_class = RandomClass.new
a = Marshal.dump(random_class)

and there you are, so one can't serialize a object with MySql object in it and with the set of finding i can even say that serializing even with yaml is also not possible.

So work around to this that i can suggest is to do a close of live mysql object in the method itself so that at any give time when object will be passed to serialization using Marshal.dump there would be no live MySql object to hinder with serialization :)

try this piece of snippet now

require "rubygems"
require "mysql"

class RandomClass
def initialize
db_instance = Mysql.real_connect("hostname", "username", "password", "database_name")
db_instance.close
end
end

random_class = RandomClass.new
a = Marshal.dump(random_class)


And this should work :)

Wednesday, February 9, 2011

How to install ruby 1.9 and rails with RVM

Just follow these steps
ruby -v
mkdir -p ~/.rvm/src/ && cd ~/.rvm/src && rm -rf ./rvm/ && git clone git://github.com/wayneeseguin/rvm.git && cd rvm && ./install
rvm install 1.9.1
rvm list
rvm 1.9.1
rvm 1.9.1 --default
rvm system
gem install tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n
gem install rails --pre
rails topscore
cd topscore
rails server
gem install sqlite3-ruby
rails generate scaffold game name:string
rake db:migrate

and you are done :)
taken from http://railscasts.com/episodes/200-rails-3-beta-and-rvm

Thursday, December 30, 2010

2 steps to install sqlite 3 on ubuntu for ruby

open a console and write two steps one by one
sudo apt-get install sqlite3 libsqlite3-dev
sudo gem install sqlite3-ruby
And you are done :)

Saturday, May 22, 2010

3 step RMagick install in rail/ubuntu

sudo apt-get install imagemagick
sudo apt-get install libmagick9-dev
sudo apt-get install libmagickwand-dev
sudo gem install rmagick

yes thats it :)

Monday, October 19, 2009

BrokenCompass: How to go about it

Once you are done with installation and index creation for brokencompass, can play around with the search capabilities of it..

Point 01. ruby script/console
==> Output
Adding BrokenCompass Version: 1.0
Loading index... [broken_compass/advertisment.index]

Point 02. Searching in all fields, i.e not using extended_mode
Advertisment.look_in_brokencompass("train")
==> Returns an inference hash like this
{
# Time elapsed in the execution(in seconds)
:time_elapsed=>0.00617599487304688,

# Field(s) which is/are available for indexing
:fields=>["location", "title", "description"],

# Weight given to different attributes to ranks result items
:column_weight=>{:description=>10, :location=>10, :title=>10},

# Count of total records found
:total_record_count=>44,

# Boolean field to unload indexes from memory once query is fired
:eager_unload=>false,

# Boolean field for extended_mode, set to true when using attribute(column) level searches
:extended_mode=>false,

# Boolean field for PLURAL forms of search word
:plural_forms=>true,

# Result set with weighted scores
:weighted_records=>[[14844, 20], [17445, 20], ... , [15148, 10]]}

# Limit to result set
:limit=>20,

# Offset to result set
:offset=>0,
}


Point 03. Searching in extended mode i.e specifically in some attribute(from the attributes used in sql query for index creation)
Advertisment.look_in_brokencompass("'job'@title", :extended_mode => true)
==> Returns an inference hash same as above
{
:column_weight=>{:description=>10, :location=>10, :title=>10},
:total_record_count=>1419,
:time_elapsed=>0.0523371696472168,
:fields=>["location", "title", "description"],
:eager_unload=>false,
:limit=>20,
:extended_mode=>true,
:offset=>0,
:plural_forms=>true,
:weighted_records=>[[14607, 20], [17685, 20], ...... , [13727, 10]]
}


Point 04. Searching in extended mode and also using other possible options
Advertisment.look_in_brokencompass("'job'@title", :extended_mode => true, :offset => 4, :limit => 5, :column_weight => {:title => 40, :description => 20})
==> Returns an inference hash same as above
{
:column_weight=>{:description=>20, :location=>10, :title=>40},
:total_record_count=>1419,
:time_elapsed=>0.0521509647369385,
:fields=>["location", "title", "description"],
:eager_unload=>false,
:limit=>5,
:extended_mode=>true,
:offset=>4,
:plural_forms=>true,
:weighted_records=>[[17947, 80], [17946, 80], [9597, 80], [12556, 80], [25975, 80]]
}

Point 05. To retrieve data results from above mentioned conditions and critriea
Advertisment.find_with_brokencompass("'job'@title", :brokencompass => {:extended_mode => true, :offset => 4, :limit => 5, :column_weight => {:title => 40, :description => 20}})
==> Returns an result-set array of type Advertisment

Point 06. To user "and" & "or" operators in extended mode
Advertisment.look_in_brokencompass("'job'@title | 'career'@description", :extended_mode => true, :offset => 4, :limit => 5, :column_weight => {:title => 40, :description => 20})
==> Returns an result-set array of type Advertisment
Advertisment.look_in_brokencompass("'job'@title & 'career'@description", :extended_mode => true, :offset => 4, :limit => 5, :column_weight => {:title => 40, :description => 20})
==> Returns an result-set array of type Advertisment

In case of any further queries please contact me at praveen[dot]kumar[dot]sinha[at]gmail[dot]com

BrokenCompass - Install instructions

BrokenCompass up and running in flat 5 mins

Step: 01
Install BrokenCompass plugin
ruby script/plugin install http://brokencompass.googlecode.com/svn/trunk
from inside of rails app root directory.

Step: 02
Add BrokenCompass configuration yml file
Create file
config/brokencompass.yml

Add content like
#brokencompass.yml............. STARTS
index-sources:
advertisment:
sql: "select id,title,description,location from advertisments"
table_name: advertisments

query:
sql: "select id,content,dynamic_fields from queries"
table_name: queries

# Database connection adapter
connection_adapter:
adapter: "mysql"
host: "localhost"
username: "username"
password: "password"
database: "myapp_development"

#brokencompass.yml............. ENDS

Note:
0. Nothing is optional in the yml(everything is mandatory)
1. Add as many items in index-sources as you want, but every item name should match model names(for which they will be used).
2. Make sure to include the Id field so that index created can be mapped to record identifier.
3. Provide the connection adapter details in connection_adapter section(so as to answer, where to read data from)
4. If you are not minding the spaces in yml(then you have messed up the configuration, so don't try to remove spaces :) from yml, wanna know more about yml? )


Step: 03
Run rake task to create indexes for the first time
rake brokencompass:create_index
from inside of rails app root directory.

Step: 04 (This is required, when one is re-creating the indexes from scratch)
Run rake task to re-create indexes
rake brokencompass:index
from inside of rails app root directory.

Step: 05 (To delete any of the indexes so created, delete corresponding "xxxx.index" file from broken_compass
from inside of rails app root directory.

Friday, October 9, 2009

BrokenCompass

New in-memory full text search engine in ruby and in rails "BrokenCompass".
Coming soon....
keep an eye on this space..

Saturday, May 2, 2009

Pretty urls with name or title in ruby on rails

Hi folks,

Something on which is very old.. might not be very interesting for many of you who are into rails development. I am talking about the pretty-urls the SEO stuff(forget all this, it is for good readability of url).

So what my basic funda that i have gathered from last three years is that, every resource is identified by an identifier in rails(generally called id, in terms of database), but displaying the id doesn't sounds legal to me.. so to hide the ids and to have a nice looking readable

we can have title or name fields in the database's relation(table) and to make use of pretty urls, we make corresponding column like stripped_title or stripped_name.
create a file "make_pretty_url.rb" in initializers in config and append the following code.


puts "===================================================="
puts "Adding engine to make pretty url(s)"
puts "===================================================="
module ActiveRecord
class Base
after_create :make_or_update_pretty_url_name

def make_or_update_pretty_url_name
parent_column = nil
stripped_column = self.attributes.collect {|x| x.to_s.include?("stripped_") ? x : nil}.flatten.compact.first
if stripped_column
parent_column = stripped_column.gsub("stripped_","")
if self.send(parent_column)
self.send("#{stripped_column}=",stripp_it(self.send(parent_column)))
if self.class.find(:all, :conditions => ["#{stripped_column} = ?",self.send(stripped_column)]).length > 0
self.send("#{stripped_column}=","#{self.send(stripped_column)}-#{self.id.to_s}")
end
self.send("save")
end
end
end

def stripp_it(str)
str.strip.downcase.gsub(/[^a-z0-9]/,"-").gsub(/(-)+/,"-").gsub(/(-)+$/,"")
end
end
end


it will automatically create stripped_{column_name} and will take care of duplicates and instead of using find_by_id(), find_by_stripped_{column_name} will be used to get the resource and "make_or_update_pretty_url_name" method will also be available for any manual work too.

Hope this just adds in the knowledge base of stuff, and if there is anything about it, let me know through comments..

Monday, February 23, 2009

SSL and RubyonRails

Hi guys and gals too :),

Am back after a fair long interval this time, ya ya same old reason was busy with office and all. But recently did something good(if not unusual).
And the heading is telling it very correct, did implemented the SSL and used https for some of my pages in rubyonrails.

As many of my readers know, i am new to ubuntu so is new to apache too(initially i used to think, these dealings are sort of system admin stuff) but hey after all it is a software and some bit of configurations.. so i thought lets do it.. and i did it...

So now trimming all the conversation.. the aim of my application was to implement https(SSL) for the "payment gateway" and "pick package" page
and here is the solution
for the prerequisite purpose i assume that you have apache and ruby on rails setup in your system and you have an ROR application which needs https protocol for some of the pages

Step 00: Create a ssl certificate signed by yourself (by following these steps)
install the ssl-cert package
sudo aptitude install ssl-cert

# to create a self-signed certificate.. it will open several dialog boxes, keep on answering the question in correct format(some 7-8 inquiries are there) and at the end you will have your self-signed certificate
sudo make-ssl-cert /usr/share/ssl-cert/ssleay.cnf /path/to/ssl/certictare/selfsigned.pem

Step 01: first of all enable the modes(for apache)
sudo a2enmod ssl
sudo a2enmod proxy
sudo a2enmod rewrite
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod headers


Step 02: set the virtual host
goto apache root directory(which is at /etc/apache2 in my case) and execute following commands
#to switch to apache directory
cd /etc/apache2

#to disable the default site(which have the default configuration of apache)
sudo a2dissite default
#create a new configuration for virtual host by copying the default site configuration and (rename 'ourapplication' with your application name)
sudo cp sites-available/default sites-available/ourapplication
#to enable our application site configuration in apache
sudo a2ensite ourapplication

Step 03: edit the newly created ourapplication configuration, which is available at /etc/apache2/sites-available/ourapplication using any of your favorite editor
sudo gedit /etc/apache2/sites-available/ourapplication


and it should look something lie this
<VirtualHost *:80>
ServerName ourapplication
ProxyPass / http://somename.com:3000/
ProxyPassReverse / http://somename.com:3000/
</VirtualHost>

<VirtualHost *:443>
ServerName ourapplication
ProxyPass / http://somename.com:3000/
ProxyPassReverse / http://somename.com:3000/
ProxyPreserveHost On
RequestHeader set X_FORWARDED_PROTO 'https'

SSLEngine On
SSLProxyEngine On
SSLCertificateFile /path/to/self/signed/certificate/selfsigned.pem
SSLProxyMachineCertificateFile /path/to/self/signed/certificate/selfsigned.pem
</VirtualHost>


Step 04: We also have to change the proxy configuration, so that proxy request can be handled as we desire it to do
sudo gedit /etc/apache2/mods-available/proxy.conf

change the setting from
previous proxy setting
<proxy>
AddDefaultCharset off
Order deny,allow
Deny from all
Allow from .example.com
</proxy>
to new proxy setting
<proxy>
AddDefaultCharset off
Order deny,allow
Allow from all
</proxy>

Step 05: To reload the new setting so that apache can follow our rule..
sudo /etc/init.d/apache2 force-reload
(but it should not give any error, and it will not if no wrong is done to it form the above mentioned procedures)
if it gives some ouitpit like this
* Reloading web server config apache2
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName

Step 06: Now all apache work is done, you had to do small amount of effort in your rails application and here they
a) install a plugin
ruby script/plugin install ssl_requirement

b) include it in the application controller so that it can use ssl using
include SslRequirement

c) to use https on any particular action of some controller use
class EcommerceController < ApplicationController
ssl_required :action_name_1, :action_name_2, :action_name_3
# some more codes..............
end


if you have any query just drop me a comment, otherwise all is well as expected :)

Tuesday, February 3, 2009

form inside form. is it possible?

Hi guys,

whatever i can recall i think i got hit by this problem earlier too, but at that time certainly i was not a blogger, in fact i was lazy of writing too :)) and see it came again, this issue is of placing a html form inside another.
It sounds messy right?? ya ya i know, but some how this was one of the requirement for one of the application that i was developing.

was having a html form with list item as radio box option but later it came to the requirement that label of radio button too should be update able so i tried putting up form_remote_tag(the rails method) for labels but it was not working in fact html form was not coming on to the page(as i was loading it through partials).

So went in for :with parameter in using the remote functions for prototype helper

the host rhtml
<% form_tag url_for(:controller => "controller_name", :action => "action_name") do %>
<%@some_collection_hash.each do |key,value| -%>
<span><%=key%></span>
<%value.each do |single_item|-%>
<div class="sbc" style="width:30px;">
<%=radio_button_tag("publisher[#{key}]", single_item.id,false,:disabled => (single_item.status != "valid")) -%>
</div>
<div class="sbc" id="<%=single_item.id-%>_batch">
<%=render :partial => "/xyz/partial_one", :locals => {:single_item => single_item, :key => key} -%>
</div>
<%end-%>
<%end-%>
<%= submit_tag "Get Summary"%>
<% end %>

The view partial
<ul>
<li class="head_li">Satus:</li>
<li class="dotless_li">
<%=select_tag("#{single_item_id}_status", options_for_select(["valid","invalid","pending","running","deleted","summarize"],@single_item.status)) %>
</li>
</ul>
<ul>
<li class="head_li">Description:</li>
<li class="dotless_li"><%=text_area_tag "#{single_item_id}_desc", @single_item.comments, :rows => 3, :cols => 30 -%></li>
</ul>
<input type="button" value="Close" onclick="<%= remote_function(:update => "options",
:url => {:controller=> :controller_name, :action => :show_single_iteminfo ,:single_item_id => single_item_id, :key => key},
:update => { :success => "#{single_item_id}_single_item", :failure => "#{single_item_id}_single_item" }) -%>" />

<input type="button" value="Save" onclick="<%= remote_function(:update => "options",
:url => {:controller=> :controller_name, :action => :save_single_iteminfo ,:single_item_id => single_item_id, :key => key},
:with => "'description='+$F('#{single_item_id}_desc')+'&'+'status='+$F('#{single_item_id}_status')",
:update => { :success => "#{single_item_id}_single_item", :failure => "#{single_item_id}_single_item" }) -%>" />

Thursday, January 22, 2009

Group controllers/views in RubyonRails

Hi guys,

It has really been a long time since i blogged about something from ruby on rails corner.. and so here is one.

Recently, have been asked to merge to rails application in one. Meaning to merge the code base so that they can become an single application but to maintain the readability i thought of keeping the code base some how segregated. So initially the idea was of making a plugin of application which is to be merged with main application and with the use of rails-engine plugin that would have been easy(at least that i thought of) but see the dilemma i was not even able to run the demo plugin(with the use of rails-engine) and as time was constraint too i thought of dropping the idea and doing some thing easy which can do the work and also in less time.

Then came the idea of grouping controller, views, helpers, layout, model etc... all in different folders and i was able to segregate controller, views but not models(anyone with solution/thought on it please comment).

Static view of segregated 'controllers'


Static view of segregated 'helpers'


Static view of segregated 'views'


Now that i have shown you the file positions.. what is the code which does the trick!! The answer is, when one creates a folder inside controller's folder in rails application, it makes a module of it and one should make the controllers belong to this module other wise those controllers will not be available.

class AuditTool::BaseController < ApplicationController

end

this acts as application controller(which is derived for the main application-controller) to the segregated controllers and other controllers could be derived from this base controller, like this


class AuditTool::BatchesController < AuditTool::BaseController
layout "/audit_tool/layouts/main"
def index
# line(s) of code.................
end
def list
# line(s) of code ...............
end
end


and helpers looks like this

module AuditTool::BatchesHelper
def function1(processed_on,label = "earlier")
# line(s) of code ...............
end
def function2(processed_on,label = "earlier")
# line(s) of code ...............
end
end


and getting spoon feeding the head section of layout goes like this

<%=stylesheet_link_tag("audit_tool/style")-%>
<%=stylesheet_link_tag("audit_tool/style_new")-%>
<%=javascript_include_tag :defaults-%>
<%=javascript_include_tag "audit_tool/slider.js"-%>
<%=javascript_include_tag "audit_tool/custom_1.js"-%>

link to controller/action of segregated controller is written like this

<%=link_to "Report Audit", {:controller => "audit_tool/reports" , :action=> "index"} -%>

link to main controller/action from inside of segregated views is written like this

<%=link_to "Got to main app", {:controller => '/admin', :action => 'dashboard' }%>


the post has started looking long, i thing i should stop it here only....
anybody with thoughts on this with any prospective do comment ( after all knowledge is sharable)

Wednesday, November 12, 2008

Conversion from mysql to sqlite database.. via rails

I faced this problem to have a sqlite database from an existing mysql.. and being the rails guy... i made it the rails way.. but of course there are other option available which will be fast .. this is kind of slow... but works for small stores :))

Steps are..
1. Create a source model which is connected with mysql connection/using mysql adaptor
class SourceDB < ActiveRecord::Base
end
SourceDB.establish_connection($config["database_mysql"])

2. Create a destination model which is connected with sqlite connection/using sqlite adaptor
class TargetDB < ActiveRecord::Base
end
TargetDB.establish_connection($config["database_sqlite"])

3. Create schema from source database so that can reproduce it in destination(sqlite) database.
File.open(name_of_schema_file,"w") do |file|
  ActiveRecord::SchemaDumper.dump(SourceDB.connection, file)
end

4. Alter the generated schema file to remove line which contains "add_index" as these are of no use in sqlite conversion process

5. Then load the altered schema file (change the ActiveRecord::Base.connection to point to destination database before loading)
ActiveRecord::Base.connection = TargetDB.connection
load(name_of_schema_file)

6. and now resurvely iterate with all tables in source database and transport it to derstination (sqlite) database

Source file [mysql_to_sqlite.rb]
require 'rubygems'
require 'active_record'
require 'active_support'
require 'sqlite3'
require 'active_record/schema_dumper'

require "yaml"
require "create_class.rb"

t1 = Time.now
$config   = YAML.load_file("config/config.yml")
name_of_schema_file = ($config["schema_file"])

class SourceDB < ActiveRecord::Base
end
SourceDB.establish_connection($config["database_mysql"])

class TargetDB < ActiveRecord::Base
end
TargetDB.establish_connection($config["database_sqlite"])

puts "dumping the schema"
File.open(name_of_schema_file,"w") do |file|
  ActiveRecord::SchemaDumper.dump(SourceDB.connection, file)
end

puts "discarding index(es)"
line_array = Array.new
File.open(name_of_schema_file,"r") do |file|
  file.each { |line|    line_array << line  unless line.include?("add_index")}
end

File.rename(name_of_schema_file, "original_#{name_of_schema_file}")

File.open(name_of_schema_file,"w") do |file|
  file.puts(line_array)
end

puts "loading the schema"
ActiveRecord::Base.connection = TargetDB.connection
load(name_of_schema_file)


SourceDB.connection.tables.each do |tbl|
  puts "Table_initiated: #{tbl.inspect}"
    SourceDB.set_table_name tbl
    SourceDB.set_inheritance_column ""
    
    create_class('TargetModel', TargetDB) do
      set_table_name tbl
      set_inheritance_column ""
    end
    puts "=========for table: #{tbl}=========="
    total_record_in_table = SourceDB.count_by_sql("SELECT COUNT(*) from #{tbl}")

    tub_size = 1000
    no_of_iteration = ((total_record_in_table % tub_size) == 0) ? (total_record_in_table / tub_size) : ((total_record_in_table / tub_size) + 1)

    for j in 0..(no_of_iteration-1)
      current_record_set = SourceDB.find(:all,:offset => (j*tub_size), :limit => (tub_size - 1))

      current_record_set.each_with_index do |record,ind|
        record_copy = TargetModel.new
        record.attributes.each do |key,value|
        record_copy.send("#{key}=",value)
        end
        record_copy.save
        puts "Completed: #{((j*tub_size) + ind)} of #{total_record_in_table}   with id: #{record_copy.id} ."
      end
    end
end
        
t2 = Time.now

puts "Process initiated at: #{t1}"
puts "Process completed at: #{t2}"
puts "Time elapsed        : #{t2-t1} seconds"

Source file [create_class.rb]
def create_class(class_name, superclass, &block)
  klass = Class.new superclass, &block
  Object.const_set class_name, klass
end

Configuration file [config.yml]
database_mysql:
  adapter: mysql
  database: databse_name
  username: root
  password: xxxxxxxx
  host: xxx.xx.x.xxx
  timeout: 5000
  encoding: utf8

database_sqlite:
  adapter: sqlite3
  database: sqlite_database_file_with_path.db
  
schema_file: schema.txt

Honda Civic & A/C problems.

Hello Friends, Got a post again on to Honda Civic (The good old favorite commuter of mine). This car has been doing great except for so...