Wednesday, December 28, 2011

Rehash method of Ruby Hash Class

Hello Guys,
        There is an interesting method provided by hash called 'rehash'. It rebuilds the hash based on the current hash values for each key. If values of key objects have changed since they were inserted, this method will reindex hash. If Hash.rehash is called while an iterator is traversing the hash, an IndexError will be raised in the iterator.

   a = [ "p", "q" ]
   b = [ "r", "s" ]
   h = { a => 100, b => 200 }
   h[a] returns 100
   a[0] = "z"
   h[a] returns nil. we have to rebuild the hash after updating key using rehash method of hash.
   h.rehash returns {["z", "q"] => 100, ["r", "s"] => 200}
   h[a] returns 100

Monday, December 26, 2011

Convert HTML to HAML and Javascript to Coffee script

Hello Guys,
        If you are using Rails 3 than as per it's standard format we have to use Haml and Coffee script instead of normal html and javascript.

There are some online tool available for conversation from one format to other.

HAML to HTML

HTML to HAML
http://html2haml.heroku.com/

JS to Coffee Script or visa versa
http://js2coffee.org/

Sunday, December 11, 2011

Generate .yml fixtures from database tables

Hello Guys,
          Wondering about how to create fixtures from tables? Here is the simple task which generate .yml from database tables.

namespace :db do
  desc 'Generate .yml test fixtures from an existing database'
  task :generate_fixtures => :environment do
    sql = "Select * From %s"
    skip_tables = ["schema_migrations"]
    ActiveRecord::Base.establish_connection
    tables = ActiveRecord::Base.connection.tables - skip_tables
    tables.each do |table_name|
      count = "0000"
      File.open("#{RAILS_ROOT}/db/fixtures/#{table_name}.yml", 'w') do |file|
        table_content = ActiveRecord::Base.connection.select_all(sql % table_name)
        file.write
table_content.inject({}) { |hash, record|
          hash["#{table_name}_#{
count.succ!}"] = record
          hash
        }.to_yaml
      end
    end
  end
end

When you invoke above task using rake db:generate_fixtures --trace. It generates all the fixtures of your database tables under db/fixtures folder of your rails application.

create_fixtures(fixtures_directory, table_names, class_names = {}) is the inbuilt method available to create fixtures for testing.

Friday, December 9, 2011

Serialize and deserialize object in ruby

Hello Rubies,
          Want to serialize and deserialize the ruby object? than use the inbuilt method of ruby.

Assume have the field says 'content' as text field of xyz table and it stores the value in serialize hash format but when we do the sql query to retrieve content than it gives output as normal string format.

content has value:
---
ruby:
  :value: 50
  :label: "ruby %"
java:
  :value: 30
  :label: "java %"
c:
  :value: 50
  :label: "c %"

Lets trying to get the content value by raw sql statement like "select content from xyz" than it return text (string) object.

now we load the object in serialize form by
puts YAML::load(content)
visa versa we have string and set to serialize object than need to use  
puts YAML::dump(content)
Same way around if you have deserialize content and want the serialize form then use content.to_yaml. It gives result in serialized form.

Thursday, December 8, 2011

undefined method 'manage_gems’ for Gem:Module (NoMethodError)

Then ruby gems will create a new file called 'gem1.8' and it will conflict with your older 'gem' file. You can find both these files in /usr/bin

So when ever you say gem list (or something with gem) it gives the error '/usr/bin/gem:11: undefined method 'manage_gems' for Gem:Module (NoMethodError)'

As a workaround,  I have followed the below steps and it worked for me. My solution was to create a symbolic between 'gem' file and 'gem1.8' file.

Steps:

first copy your 'gem' file (as a backup)

cp /usr/bin/gem /<my other path>/

Now delete the 'gem' file

sudo rm -f /usr/bin/gem

Now create the symbolic link

ln -s /usr/bin/gem1.8 /usr/bin/gem

Thats it, now run gem list and it should work.

Thursday, December 1, 2011

Use build with has_one and belongs_to association

Suppose we have association between two models as has_one and belongs_to. We want to create the child record with parent record using build method than need to follow below easy steps.

eg. Class User < ActiveRecord::Base
        has_one :profile
      end
      
     Class Profile < ActiveRecord::Base
        belongs_to :user
     end
   
When we are creating the user than required to create associated profile by using below methods:

@user.build_profile or
@user.profile.build

It create associated profile record along with user.