Friday, July 23, 2010

Few useful links for pdf generation in rails

http://www.accesspdf.com/pdftk/#packages

http://wiki.github.com/sandal/prawn/using-prawn-in-rails
http://railscasts.com/episodes/153-pdfs-with-prawn
http://wilsoncolab.com/blog/bryan/2008/11/30/writing-pdfs-with-ruby-on-rails-using-prawn-and-prawnto/
http://prawn.majesticseacreature.com/
http://www.cracklabs.com/prawnto/use

http://rdoc.info/rdoc/sandal/prawn-layout/blob/bcfac3efffb68a0b3c9f1007c1c1193443470afc/Prawn/Document

Thursday, July 15, 2010

Modifications in rails auto_complete plugin to pass id as param.

The auto_complete plugin explain in railscasts passes the selected name as param.
Rails Autocomplete
In most cases we want to pass the select value id instead of name.
To pass the id instead of name in the above link all we need to do is


<!-- categories/index.js.erb -->
<%= auto_complete_values @categories, :name, :id %>
<%= text_field_with_auto_complete :product, :category_name, { :size => 15 }, { :url => categories_path(:js), :method => :get, :param_name => 'search', :after_update_element => "function(text_field, li_element){$('product_category_id').value = $('text_' + li_element.id).value;}"}) %>
<%= hidden_field "product", "category_id" %>
 
<!-- application_helper -->
  def auto_complete_values(entries, text_field, value_field, phrase = nil)
    return unless entries
    items = entries.map do |entry|
      content_tag("li", phrase ? highlight(entry[text_field], phrase) : h(entry[text_field]), {:id => entry[value_field]}) +
      hidden_field_tag("text_#{entry[value_field]}", entry[value_field])
    end
    content_tag("ul", items.uniq, {:style=> "height: 150px; overflow:auto;"})
  end

Note: In case you want to trigger an event on select of autocomplete list then all you want to do is modify after_update_element
Ex: <%= text_field_with_auto_complete :company, :number, {:class => "title"}, {:url => autocomplete_company_numbers_path(:js), :method => :get, :param_name => 'search', :after_update_element => "function(text_field, li_element){window.location.href='/company__numbers/show/' + $('text_' + li_element.id).value;}"} %>

Tuesday, July 13, 2010

Using Clockwork and Delayed Job gem to send event reminder emails.

code below run a background process to send an event reminder emails.
Install clockwork and delayed_job gem. You might want to upgrade ruby gem version too
commands:
gem install clockwork
gem install delayed_job

Create Reminder model with event_id, reminder_at fields

class CreateReminders < ActiveRecord::Migration
  def self.up
    create_table :reminders do |t|
      t.integer :event_id
      t.datetime :reminder_at
      t.timestamps

    end
  end

  def self.down
    drop_table :reminders
  end
end


Add the association into Reminder model
belongs_to :event

Now create clock.rb in the lib folder of your application and add the following.

  require 'rubygems'
require 'clockwork'
include Clockwork

require 'config/boot'
require 'config/environment'
every(2.minutes, 'reminder.deliver') {
reminders = Reminder.find(:all, :conditions => ["reminder_at <= ? and reminder_at > ?", Time.now.advance(:minutes => 2), Time.now])
#reminders = Reminder.find(:all, :conditions => ["reminder_at <= ?", #Time.now.advance(:minutes => 2)])
unless reminders.nil?
UserMailer.send_later( :deliver_event_reminder, reminders )
end


Now using command prompt go to the root directory of the application and run the command
clockwork lib/clock.rb
If you are using delayed_job then you will have to  rake jobs:work in another command prompt
This will trigger an event every 2 minutes which will get all the reminders that needs to be send in the next two minutes and delivers it.

How to make your pc run as fast a new

STEP 1

For those who believe their computer lags at start up, a quick look at the MSConfig would be the place to start. MSConfig can be accessed by hitting the Windows button on keyboard along with R key.

This will bring up the 'Run' dialog. Type 'MSConfig' and you will get the 'Start Up' tab with a list of programs and executables that are launched when the computer starts up.

Starting up of too many programs when the computer boots could be a reason why the boot time is so long. The remedy would be to go through the entire list and uncheck the unnecessary items.

If there are entries you are not sure about, check them on the internet, as unchecking the wrong entries could potentially mess up the functionality of some of your programs.

STEP 2

Defragment the hard drive once a month. It's like changing your car's oil - it should be the one thing on your list even if you don't do anything else. Windows stores information about your programs in the Registry.

This can get cluttered and affect performance. The solution is to download free registry cleaners from the internet (eg CCleaner) to remove redundant program entries.

STEP 3

Indexing Services is a nifty little program that uses a large amount of RAM. This processes indexes and updates lists of files on your computer. This is done so that when you search for something, it can search faster by scanning the index lists.

So, if you don't search your computer often, turn it off would better performance. Simply go to 'Start' button on taskbar, click on 'Control Panel' and select 'Add/Remove Programs'. Find the tab 'Add/Remove Window Components' and uncheck 'Indexing Services'. Click Next.

Also, if there is a delay every time you open 'My Computer' to browse folders, try this.

Open 'My Computer', click on 'Tools', select 'Folder Options', click on the 'View' tab to uncheck the 'Automatically search for network folders and printers' box.

Click 'Apply' and then reboot your computer for changes to apply.

STEP 4

Hard drive performance plummets as you near the drive's maximum capacity.

Assuming you don't have an additional drive to move the content to, your choices are slim. But before you take a machete to your files, you may want to compress them.

Use the built-in compression tool in Windows, which makes accessing the files no different from it currently is. Go to 'Disk Cleanup' and make sure 'Compress Old Files' is checked.

Click 'Options' and specify the age of the files you want Windows to compress - Windows will compress only the files you haven't accessed in more than six months (or as specified).

STEP 5

An important feature of Windows is its ability to return system files to the state they were in earlier. Before tweaking, users can create a new 'Restore Point'.

Go to Control Panel>Performance and Maintenance>System Restore or Start-All Programs > Accessories > System Tools>System Restore and select 'Create a restore point'.

If it is left alone, this can consume a good portion of your disk space with unnecessary restore points.

By default, it uses up to 12 per cent of each of your drives and, even if you have a big hard drive, many extra restore points can slow down processes like virus checking, disk-defragmenting, etc.

Typically, how much space you should allot to system restore depends on your system (200 MB - the minimum allowed - will hold 5 or 6 restore points) and should suffice for average users.

STEP 6

Also, the Windows XP (and Windows Vista) computers have features like smooth animated menus and transparent windows.

These use processor resources, too, which means they can cause slowdown. Right-click the 'My Computer' icon and click 'Properties'.

Click the 'Advanced' tab and under 'Performance', click 'Settings'. Choose to switch off some of these effects and see the improvement in your PC's performance.

STEP 7

Accumulated dust in computer cases a reason for over-heated PCs. Dust gets into the ball bearings in the fan and cause the fan to stop working. This may lead to overheating of processor and permanent hardware failure.

It is less common, but static charges from dust can also be a threat. A layer of dust on a memory chip can cause static electricity to build up.

This can amount to electric charge, which discharges on to your motherboard or memory chip. The best way to stop dust from building up is to open the case on your PC and vacuum it.

We recommend never tweak without knowing how to return where you began

Wednesday, July 7, 2010

Monday, June 28, 2010

Integrate Rails with CouchDB using ActiveCouch

This document assumes you have CouchDB 0.8.1 + as well as Rails 2.1.1 + installed.
To install CouchDB please refer Installing CouchDb on windows
.
So a lot of the evolution of ActiveCouch correlates with the level of frustration with the library and making it better implies making our lives better. There are maybe better/more-lightweight wrappers for CouchDB out there.

Install ActiveCouch as a Rails Plugin

The biggest advantage of ActiveCouch v0.2.x is that it can be used as a Rails plugin as well as, as a standalone gem. You can install activecouch gem by the command
$ gem install activecouch

ActiveCouch uses the JSON gem for fast JSON serialization/deserialization, so install that first:

$ gem install json
Then, create your Rails project

$ rails couchify
Then, install ActiveCouch as a plugin:


$ cd couchify
$ ./script/plugin install git://github.com/arunthampi/activecouch.git
This installs ActiveCouch in RAILS_ROOT/vendor/plugins

Follow up – Get it started

You need to create a file called activecouch.yml in RAILS_ROOT/config which looks something like this (Of course, depending on your deployment strategy, your test and production sites will be something else)

development:
  site: http://localhost:5984/
test:
  site: http://localhost:5984/
production:
  site: http://localhost:5984/ 

Create your ActiveCouch model

This is now very simple in ActiveCouch. At the root of your Rails project, simply enter
$ ./script/generate activecouch_model cat
This generates a cat.rb file in RAILS_ROOT/app/models which looks something like this:

class Cat < ActiveCouch::Base
    site YAML::load(File.open(File.join(Rails.root,
                  'config', 'activecouch.yml')))[Rails.env]['site']
end
All set to use CouchDB database depending on your Rails environment. Now ActiveCouch assumes that you already have a database called ‘cats’ created in CouchDB, so that you can start creating all your CouchDB documents. No matter, Rake tasks to the rescue:

Creating your database

If you goto RAILS_ROOT of your app, and enter the following:

$ rake --tasks
You will see some of the following:

rake activecouch:create_db           # Creates a database in CouchDB
rake activecouch:delete_db           # Deletes a database from CouchDB
rake activecouch:delete_view         # Deletes a view in CouchDB
rake activecouch:save_view           # Saves a view in CouchDB
So to create your database, just enter

$ rake activecouch:create_db db=cats
And boom, you have your database called cats created in ActiveCouch.
If for some reason, you need to delete your database, the answer is simple:

$ rake activecouch:delete_db db=cats
Awesome, now you can go ahead and edit your model to have various fields, for example a cat should probably have a name, so your model will look something like this:

class Cat < ActiveCouch::Base
    site YAML::load(File.open(File.join(Rails.root,
                  'config', 'activecouch.yml')))[Rails.env]['site']

    has :first_name
    has :last_name
end

Creating your first ActiveCouch Objects

To see if this works, open up ./script/console and type in the following:

> cat = Cat.new(:first_name => "Mr.", :last_name => "Jinks")
> cat.save
Or

> cat = Cat.create(:first_name => "Mrs.", :last_name => "Jinks")

Extract db data

CouchDB lets you query your database using something called Views. The CouchDB wiki has a lot of information on how views work. You should also probably consider watching Jan Lenhardt’s awesome talk on CouchDB , which explains a lot of the differences between Relational Databases and Document-Oriented Databases.
ActiveCouch tries to abstract a lot of the view behaviour from you (some people have said it is inelegant , so I’m hoping for some better ideas from people reading this). For example, for the Cat model above, suppose you want to query for all cats by their last name, you can create a view at /cats/design/by_last_name/by_lastname and ActiveCouch lets you query your CouchDB database like so:

cats = Cat.find(:all, :params => {:last_name => "Jinks"})
The price you pay for the somewhat unwieldy view names, is that you get very convenient find methods. Of course if you have created your custom views, you can always use the find_from_url method which works something like this:

cats = Cat.find_from_url("/cats/_view/by_last_name/by_last_name?key=%22Jinks%22")
Ok, back to views. ActiveCouch lets you create your views using a very simple generator. So from your RAILS_ROOT again,

$ ./script/generate activecouch_view ByLastName
This creates an ActiveCouch::View class in RAILS_ROOT/app/models. You can edit the view as given below, so that you can start querying by last name:


class ByLastName < ActiveCouch::View
  define :for_db => 'cats' do 
    with_key 'last_name'
  end
end
Done. But this view is not yet in the CouchDB database, so you need to save it into the database.

Saving your view

To save your view, you can use the built-in Rake task to save your views:

$ rake activecouch:save_view db=cats view=ByLastName
And boom, your view is saved.
Now suppose you want your view to be saved in many databases inside CouchDB. i.e. you have databases such as dogs, people, etc. and all of them need to be queried by their last name. No matter, you can use db=_all_dbs as part of your Rake task and this will save the view into all databases.

$ rake activecouch:save_view db=_all_dbs view=ByLastName
Now you are ready to start querying your database and finding objects.

Installing CouchDB

CouchDB for windows is available at
http://shockdragon.com/couchdb/couchdb-0.9-windows.zip
Simply download it and then extract the zip and execute 'couch_start.bat' from the bin directory, then Futon will be available at
http://127.0.0.1:5984/_utils
 
For more information refer the links:
http://wiki.apache.org/couchdb/Installing_on_Windows
http://wiki.apache.org/couchdb/Windows_binary_installer
http://people.apache.org/~mhammond/dist/snapshots/
http://people.apache.org/~mhammond/dist/0.11.0/

Thursday, June 24, 2010

Prawnto to generate pdf documents.

You need to install the following
gem install prawn
ruby script/plugin install git://github.com/thorny-sun/prawnto.git

Now add a link in your view

<%= link_to "Printable Invoice (PDF)", order_path(@order, :format => 'pdf') %>

Your controller

prawnto :prawn => { :top_margin => 75 }

def show
  @order = Order.find(params[:id])
end
 
create show.pdf.prawn in your views and add
 
pdf.text "Order ##{@order.id}", :size => 30, :style => :bold

pdf.move_down(30)

items = @order.cart.line_items.map do |item|
  [
    item.product.name,
    item.quantity,
    number_to_currency(item.unit_price),
    number_to_currency(item.full_price)
  ]
end

pdf.table items, :border_style => :grid,
  :row_colors => ["FFFFFF","DDDDDD"],
  :headers => ["Product", "Qty", "Unit Price", "Full Price"],
  :align => { 0 => :left, 1 => :right, 2 => :right, 3 => :right }

pdf.move_down(10)

pdf.text "Total Price: #{number_to_currency(@order.cart.total_price)}", :size => 16, :style => :bold
 
For more information refer: http://railscasts.com/episodes/153-pdfs-with-prawn
For more information on prawnto refer: http://www.cracklabs.com/prawnto 

Wednesday, June 16, 2010

Using Faster CSV to parse txt files

You can parse txt files using the following code
count = 1
    ActiveRecord::Base.connection.execute("TRUNCATE your_table")
    @parsed_file = FasterCSV.parse(params[:import][:file].read).each do |row|
      if count == 0              
        unless row[0].nil?
          data = row[0].split "\t"
          self.create(:attribute1 => data[0], :attribute2 => data[1], :attribute3 => data[2])
          table_data << data
        end
      end
      count = 0
    end

Tuesday, June 1, 2010

Paperclip plugin Errno::EACCES Permission denied on windows

If you get the following error on windows while using paperclip plugin
[paperclip] Saving attachments.
[paperclip] deleting C:/Documents and Settings/Sandeep/My Documents/NetBeansProjects/logicare/trunk/public/system/datas/208/original/WLHBSITEAM.csv
  [4;36;1mSQL (0.0ms) [0m   [0;1mROLLBACK [0m

Errno::EACCES (Permission denied - C:/Documents and Settings/Sandeep/My Documents/NetBeansProjects/logicare/trunk/public/system/datas/208/original/WLHBSITEAM.csv):
  c:/ruby/lib/ruby/1.8/fileutils.rb:1297:in `unlink'
  c:/ruby/lib/ruby/1.8/fileutils.rb:1297:in `remove_file'
  c:/ruby/lib/ruby/1.8/fileutils.rb:1305:in `platform_support'
  c:/ruby/lib/ruby/1.8/fileutils.rb:1296:in `remove_file'
  c:/ruby/lib/ruby/1.8/fileutils.rb:771:in `remove_file'
  c:/ruby/lib/ruby/1.8/fileutils.rb:549:in `rm'
  c:/ruby/lib/ruby/1.8/fileutils.rb:548:in `each'
  c:/ruby/lib/ruby/1.8/fileutils.rb:548:in `rm'
  app/controllers/nhs_datas_controller.rb:54:in `update'
  app/controllers/nhs_datas_controller.rb:53:in `update'

Steps to fix the problem
1) create patches.rb in lib folder and add these lines
require 'tempfile'
class Tempfile
  def size
    if @tmpfile
      @tmpfile.fsync
      @tmpfile.flush
      @tmpfile.stat.size
    else
      0
    end
  end
end
2) Add require "patches.rb" in environment.rb inside Rails::Initializer.run
3)Make sure you have installed http://www.imagemagick.org/script/binary-releases.php#windows
4) Restart the server and you should be able to add, delete and update files.

 There is another possibility why you will get permission denied error. This is when u have the file related data saved in associated tables. Say users table has data_id and there is files table has the related data i.e data_file_name, data_content_type etc.
In this scenario when you try you update the file then you will get permission denied errors. One possible way to fix this issue is.
1) first upload the new file
2) Then update the related table i.e users table with data_id . Now the link to old data is lost.
3) You have the old file id as params while submit. so using the get the file and the delete it.
Sample code
@data = Data.new
    @data.data = params[:data][:data]
    @data.save
    @user = User.find_by_data_id(params[:id])
    @user.data_id = @data.id
    @user.save
    @old_data = Data.find(params[:id])
    @old_data.destroy
Let me know if there is any better way to fix this issue.

Monday, May 31, 2010

How to get file extension in ruby and jquery

To get the file extension excluding the "." then do the following
@file_extension = file_name.split('.').pop()

To get the file extension along with "." then do the following
@file_extension = file_name.scan(/\.\w+$/)

Using Jquery
var ext = $('#file_field_id').val().split('.').pop().toLowerCase();

Thursday, May 27, 2010

Access to /etc/hosts denied while modifying host file

This problem occurs when you  do not have the administrative rights.
You get an error when you try to edit the LMHOSTS or HOSTS file under Windows Vista.
Access to C:\WindowsSystem32\drivers\etc\ hosts was denied
or
Cannot create the C:\Windows\System32\drivers\etc\hosts file.
Make sure that the path and file name are correct.

To solve this problem:
1. Click Start–>All Programs–>Accessories–>Notepad–>Run as administrator.
If you are prompted for an administrator password or for a confirmation, type the password, or click Allow.
2. Open the Hosts file or the Lmhosts file, make the necessary changes, and then click Save on the Edit menu.

Friday, May 21, 2010

How to make dynamic on focus popup in the grid.

If you have a data table and need to display a on focus popup which shows the related columns content then you can do the following:
<% @objects.each do |object| %>
           
                   <%=h object.name%>                 
                  Show
                   
                       
           
<% end %>
<% end %>

Friday, April 23, 2010

Using annotate_models plugin

download or install annotate_models plugin (http://github.com/ganeshprasad/annotate_models) into vendor/plugins folder.
Now whenever you run the command rake annotate_models it will update the models with the schema.
Schema is also defined in ActiveRecord models whenever rake db:migrate is done.


Add a comment summarizing the current schema to the top or bottom of each of your…
* ActiveRecord models
  * Fixture files
  * Tests and Specs
  * Object Daddy exemplars
  * Machinist blueprints
The schema comment looks like this:
# == Schema Info
   #
   # Table name: line_items
   #
   #  id                  :integer(11)    not null, primary key
   #  quantity            :integer(11)    not null
   #  product_id          :integer(11)    not null
   #  unit_price          :float
   #  order_id            :integer(11)
   #

    class LineItem < ActiveRecord::Base
      belongs_to :product
     . . .

Thursday, April 22, 2010

Reading huge csv or xls file and importing the data into database tables.

To implement this we need to gems fastercsv (gem install fastercsv) and ar-extensions ( gem install ar-extensions)

FasterCSV is intended as a replacement to Ruby‘s standard CSV library. It was designed to address concerns users of that library had and it has three primary goals:
  1. Be significantly faster than CSV while remaining a pure Ruby library.
  2. Use a smaller and easier to maintain code base. (FasterCSV is larger now, but considerably richer in features. The parsing core remains quite small.)
  3. Improve on the CSV interface. 
Advantages:

  • FasterCSV has a stricter parser and will throw MalformedCSVErrors on problematic data.
  • FasterCSV has a less liberal idea of a line ending than CSV. What you set as the :row_sep is law.
  • CSV returns empty lines as [nil]. FasterCSV calls them [].
  • FasterCSV has a much faster parser. 

ar-extensions

ActiveRecord::Extensions provides extensions to:
  • mass import data
  • convert ActiveRecord models and arrays to CSV format
  • work with temporary tables
  • control foreign keys
  • to include extensible features for better and custom finder support
Sample code to implement :
require 'rubygems'
require 'fastercsv'
require 'ar-extensions'
table_columns = [:attribute1,:attribute2, :attribute3, :attribute4, :attribute5 ]
table_data = Array.new


FasterCSV.foreach("#{RAILS_ROOT}/public/data/yourcsvfile.csv") do |row|
  row.delete_at(14)   #this can be used incase you want to delete any unnecessary column from any row of csv data

  table_data << row
end
options = { :validate => false }
CareHomeHq.import table_columns,table_data

Here insert of data will also be faster because we are not creating a new instance of CareHomeHq everytime a new row is fetched. Instead all the rows are saved in an array first and then we use ar-extensions to import the data.


PARSING FROM TEMP FILE
In case you want to parse from temp file then all you need to do is
  @parsed_file = FasterCSV.parse(params[:data_import][:file].read).each do |row|   
       puts row.inspect
      #your code here     
   end

Monday, April 5, 2010

Conditional validations in rails

By default, validations will take place every time the model is saved. Sometimes you only want a validation to happen when certain conditions are met. See how to do that in this episode.

# models/user.rb
validates_presence_of :password, :if => :should_validate_password?
validates_presence_of :country
validates_presence_of :state, :if => :in_us?
attr_accessor :updating_password

def in_us?
country == 'US'
end

def should_validate_password?
updating_password || new_record?
end

# in controller
@user.updating_password = true
@user.save

# or...
@user.save(false)

Monday, March 1, 2010

Ruby: Large xml files Parsing With SAX

SAX is an event-driven parser for XML.

It sequentially reads the xml and generates special events. So, if you want to use SAX, you should implement the code to handle them. It's quite different from the DOM model, where the whole xml is parsed and loaded in an tree.

The Ruby XML Library

The Ruby core library has a built-in XML parser (both DOM and SAX) called REXML, but it's terribly slow, it's highly advisable to use libxml. It's a binding to the popular library from Gnome and it was released as gem.

Installing libxml is simple by running the following command.

gem install libxml-ruby

Refer the following example which reads a large xml file and inserts the data into the corresponding database table.

require 'xml/libxml'
CombinationPackInd.benchmark("Truncating look up tables and inserting new records") do
#truncate combination_pack_ind table
ActiveRecord::Base.connection.execute("TRUNCATE combination_pack_ind")
#truncate combination_prod_ind table
ActiveRecord::Base.connection.execute("TRUNCATE combination_prod_ind")
############################# ---TRUNCATING ENDS--- ########################

class Lookuphandler
include XML::SaxParser::Callbacks
def on_start_element_ns (name, attributes, prefix, uri, namespaces)
@tag_name = name
@main_tag_name = "" if @main_tag_name.nil?
@main_tag_name = case name
when "COMBINATION_PACK_IND"
then "COMBINATION_PACK_IND"
when "COMBINATION_PROD_IND"
then "COMBINATION_PROD_IND"
else
@main_tag_name
end
end

def on_end_element_ns (name, prefix, uri)
@end_element = name


#saving combination pack ind
if @main_tag_name == "COMBINATION_PACK_IND"
if @tag_name == "CD"
@cd = @value
elsif @tag_name == "DESC"
@desc = @value
end

if @end_element == 'INFO'
@comb_pack_ind = CombinationPackInd.new(:DESC => @desc)
@comb_pack_ind.CD = @cd
@comb_pack_ind.save
# clear the variables so that it wil not carried to the next instance.
@cd, @desc = ""
end
end
#saving combination prod ind
if @main_tag_name == "COMBINATION_PROD_IND"
if @tag_name == "CD"
@cd = @value
elsif @tag_name == "DESC"
@desc = @value
end

if @end_element == 'INFO'
@comb_prod_ind = CombinationProdInd.new(:DESC => @desc)
@comb_prod_ind.CD = @cd
@comb_prod_ind.save
# clear the variables so that it wil not carried to the next instance.
@cd, @desc = ""
end
end

##################################### CLEARING VARIABLE CONTAINING TAG INFO AFTER INSERTING RECORD ##################################
@tag_name = ""
@main_tag_name = "" if @end_element == ( "COMBINATION_PROD_IND" || "COMBINATION_PACK_IND")
end

def on_characters(s)
@value = s
end

end
################# ---LOOKUP PARSING BEGINS--- #########################################################
file_path = RAILS_ROOT + "/data/file.xml"
parser = XML::SaxParser.file(file_path)
parser.callbacks = Lookuphandler.new
parser.parse
end

Thursday, February 25, 2010

Using REXML to read small xml files and load into database table

Save the following code in lib folder of your application as read_xml_file.rb . using command prompt run " ruby script/runner lib/read_xml_file.rb". The following code is possible to read only small xml files ( usually in kbs).


require 'rexml/document'
require 'rubygems'
include REXML
#def import_xml(tag)
file= File.new(RAILS_ROOT + "/file.xml")
doc= Document.new(file.read)

Ingredient.benchmark("Truncating ingredient table and inserting new records") do
#truncate ingredient table
ActiveRecord::Base.connection.execute("TRUNCATE ingredient")
# read table contents
XPath.each( doc, "//INGREDIENT_SUBSTANCES//ING" ){|ingredient|
ingredient_params = {}
#if there are more than one attributes for table then create a hash.
ingredient.children.each do |child|
key = "#{child.name}".to_sym
if key.to_s == "ISID"
@isid_value = child.text
else
ingredient_params[key] = child.text
end
#@desc_value = child.text if key.to_s == "DESC"

end
#create new instance
ingredient = Ingredient.new(ingredient_params)
#set the primary key if mass assigning of primary key is not possible.
ingredient.ISID = @isid_value
#save
ingredient.save
}
end

TRUNCATE All Tables in a Ruby on Rails

There might be a case where you need to delete all the rows from all the tables in the database to end up with just the structure(a bare DB). The right thing to do is to run the TRUNCATE command on all the tables. It will delete all the data and also reset the auto increment value. But the only current way to get a bare DB in rails now is to run rake db:reset. This will run rake db:drop, rake db:create and rake db:migrate. The downside of this is that if you have a reasonable number of migrations this command can take quite some time to run which is really not efficient when you are iterating fast. Hence following rake task will
TRUNCATE all the tables.

namespace :db do
task :load_config => :rails_env do
require 'active_record'
ActiveRecord::Base.configurations = Rails::Configuration.new.database_configuration
end

desc "Create Sample Data for the application"
task(:truncate => :load_config) do
begin
config = ActiveRecord::Base.configurations[RAILS_ENV]
ActiveRecord::Base.establish_connection
case config["adapter"]
when "mysql"
ActiveRecord::Base.connection.tables.each do |table|
ActiveRecord::Base.connection.execute("TRUNCATE #{table}")
end
when "sqlite", "sqlite3"
ActiveRecord::Base.connection.tables.each do |table|
ActiveRecord::Base.connection.execute("DELETE FROM #{table}")
ActiveRecord::Base.connection.execute("DELETE FROM sqlite_sequence where name='#{table}'")
end
ActiveRecord::Base.connection.execute("VACUUM")
end
rescue
$stderr.puts "Error while truncating. Make sure you have a valid database.yml file and have created the database tables before running this command. You should be able to run rake db:migrate without an error"
end
end
end


Just create a file named db_truncate.rake in your lib/tasks directory
with this code in it. Save the file and then run rake db:truncate. Your
database now should have no data now. Before you run the task make sure
that you have a valid database.yml file and have created the database
tables before running this command. You should be able to run rake
db:migrate without an error

In case you want to truncate a single table from database all you have to do is
ActiveRecord::Base.connection.execute("TRUNCATE table_name")



Monday, February 15, 2010

Avoid validations on create or update in rails

There might be several situations where we might have to avoid the default validations defined in the model when we update or create a record.
a.)One solution to avoid validation is by doing something like this
@something = Something.find(params[:id])
@something.column_save = 'your data'

respond_to do |format|
if @something.save(false) #save without validation
b.)Another solution is to write a separate method to validate in the model
please see the example below
validate :validates_uniqueness_of_name

def validates_uniqueness_of_name
return if deleted == true
num_duplicates = self.class.count(:conditions => ["name = ? AND deleted = ?",self.name, false])
if num_duplicates > 0
errors.add(:name, :taken)
end
end