Partials every-where

March 3, 2010 at 11:57 am (Partials) (, , , )

Hi everybody,

Partials are not limited to be used for HTML rendering.

They can be used for XML too.

Here is an example file ( _details.builder )
xml.name user.name
xml.gender user.gender
xml.age user.age

This can be used in another regular builder file as follows:
xml.instruct! : xml, :version=>”1.0″
xml.users {
for user in users
xml <“users/details” , :locals=>{:user=>user})
end
}

Hope, this comes handy :-)

Permalink 1 Comment

mysqldump multiple/selected tables

June 23, 2009 at 4:27 am (mysql) (, , , , )

This post explains how to dump selected tables only.
The actual syntax being:
mysqldump [ -u ] DATABASE_NAME [TABLES_LIST] > FILE_NAME

So, to dump the entire database use,
mysqldump -u USER_NAME DATABASE_NAME > FILE_NAME

To dump only selected tables use,
mysqldump -u USER_NAME DATABASE_NAME TABLE_1 TABLE_3 TABLE_4 TABLE_9 > FILE_NAME

Permalink 1 Comment

Mysql gem installation fix in rails

June 18, 2009 at 8:00 am (Uncategorized)

Today when i was trying to install mysql gem on the rails 2.2.2 + ruby 1.8.6 + gem 1.2.0 on FEDORA, i had the following error:
———————————————————————————————–
Building native extensions. This could take a while…
ERROR: Error installing mysql:
ERROR: Failed to build gem native extension.

/usr/bin/ruby extconf.rb install mysql — —with-mysql-config=/usr/local/mysql/bin/mysql_config
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lm… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lz… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lsocket… no
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lnsl… yes
checking for mysql_query() in -lmysqlclient… no
*** 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=/usr/bin/ruby
–with-mysql-config
–without-mysql-config
–with-mysql-dir
–without-mysql-dir
–with-mysql-include
–without-mysql-include=${mysql-dir}/include
–with-mysql-lib
–without-mysql-lib=${mysql-dir}/lib
–with-mysqlclientlib
–without-mysqlclientlib
–with-mlib
–without-mlib
–with-mysqlclientlib
–without-mysqlclientlib
–with-zlib
–without-zlib
–with-mysqlclientlib
–without-mysqlclientlib
–with-socketlib
–without-socketlib
–with-mysqlclientlib
–without-mysqlclientlib
–with-nsllib
–without-nsllib
–with-mysqlclientlib
–without-mysqlclientlib

Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/mysql-2.7/gem_make.out
ERROR: could not find gem — locally or in a repository
ERROR: could not find gem —with-mysql-config=/usr/local/mysql/bin/mysql_config locally or in a repository

————————————————————————————————-

The fix for it is:
gem install mysql -- --with-mysql-config=/usr/bin/mysql_config

Tats it. Its working fine for me.
Hope it works 4 oder folks too.

If not, it means that a library called mysql-devel is missing.

So, install it by “sudo yum install mysql-devel” or “sudo apt-get install libmysql++-dev”
And then, install the mysql-gem. This should for sure fix it.

For further info, checkout: Rails Mysql wiki

Permalink Leave a Comment

Making python widgets work in KDE

June 17, 2009 at 1:38 pm (Uncategorized) (, , , , )

Today i have installed KDE on my fedora machine and am having all d fun at its UI and sexy look.
Every love story has an obvious villain, and in my case when i was installing some WIDGETS, especially python based, like:
1) Now do this
2) Gmail Notifier
I had this error:
missing python script-engine errors.
On looking around the web, i could this solution:
All u gotta do is : yum install kdebase-workspace-python-applet
Daz it.

For other Linux-flavours, the solutions are available at : KDE-LOOK

Permalink Leave a Comment

Undefining class at runtime

June 16, 2009 at 6:58 am (Model) (, , , , , , , )

This post explains undefining the class at runtime.

Im not gonna explain the live-cases where this might be used. Ask Uncle Google for that.

Lets jump in at d code directly.

When u create a new class, say “Person”, two things happen:

  1. A new object of type(class) Class is instantiated.
  2. A new constant called Person will be instantiated and will hold the value of the class-object created above.

Proof: Class Person;end;

Try, Person.class #=> Class. This means that the Person holds a value which is of class Class. (1 is proved).

Try, Person = 123 #=> Curses saying, Person is already initialized, which means Person is a constant. (2 is proved).

Now, All the constants(Person) and other are silently ducked into the class Object.

Proof: Object.class_eval{Person} #=> Person Class

So, we should understand by now that the newly created class Person resides as a constant of the class Object.

So if we could somehow remove that constant, then the class Person will also be removed.

There is a method called remove_const for the class Object.

So, try Object.remove_const : Person #=> Curses, NoMethodError: private method `remove_const’ called for Object:Class

This is becoz, remove_const is a private method of Object class.

So the worry now is, how to invoke a private method.

We have two solutions:

  1. Object.send(:remove_const , : Person)
  2. Object.class_eval{remove_const : Person}

Now try, Person.new. # Curses, uninitialized constant Person

Voila, job done. Very rarely does an ERROR MESSAGE makes us feel happy. Well, kind of an article.

You can learn much about class_eval from this very good article by Khaled.

Permalink Leave a Comment

add attribute at runtime

June 16, 2009 at 6:23 am (Model) (, , , , , , )

I’m all excited abt this post.

Coz this is something i learnt just now, and consider it my best learning till now in RUBY.

Q: How to add attributes to a class at runtime?

A:

class Class
def new_attr (attr_name)

class_eval {attr_accessor :"#{attr_name}"}
end
end

class_eval : Evaluates a string or a block in the context of the receiver, here the class.

Explanation:

1) We defined a new method to the CLASS called new_attr, which is intended to add new attributes at runtime.

2) This method needs the name of the new attribute to be passed.

3) In the definition, it calls class_eval, which means “DO SOMETHING AT THE CLASS LEVEL”

4) What to do is specified in the block after the class_eval, which says : attr_accessor “#{attr_name}”, which will become attr_accessor :name when we call new_attr(‘name’).

5) We know that attr_accessor is rails syntactic sugar to create the a specific instance variable, and its set/get (read/write) methods. So here, when we call new_attr(‘name’), its creating a new instance variable (@name) and 2 new methods ‘name” and “name=”.

Try this;

class Person; end;

This creates a bare class Person.

p = Person.new

Try, p.name, it throws an error cursing “Undefined method name”.

Its fairly expected coz, Person has no attribute called name.

Now, say Person.new_attr(‘name’)

now try, p.name, it will not curse you.

To check, try Person.singleton_methods.include?(“name”)

This says TRUE.

Hope this article comes handy.

Permalink 1 Comment

Rails multiple databases

May 27, 2009 at 12:02 pm (Model) (, , , , )

Consider the case where some tables exist in database A and the others in B. Depending on the table under question, we have to connect to the specific Database and query it.

Supposing you have two databases say A,B.
Now you need configurations for both of them in database.yml (AS each RAILS configuration can point only one DATABASE)
let the development environment point database “A”
So create another environment called “dummy” point at database “B”

development:
adapter  : mysql
username : user-name
password : password
database : A

dummy:
adapter  : mysql
username : user-name
password : password
database : B

Consider two models:
a) Animal – table = animals in Database A.
b) Person – table – persons in Database B.

Usually the classes will be like follows:

class Person << ActiveRecord::Base
………
end

class Animal << ActiveRecord::Base
………
end

Now for Rails to know that it has to connect to DATABASE B when it needs to fetch data from Table “persons”, you hust have to add a line in the Person model. i.e:
establish_connection :dummy
where,
establish_connection  – method which connects to the Database specified in the environment
dummy – environment name

Thats it.
Try the following in console:

>> Animal.connected? #=> false
>> Animal
>> Animal.connected? #=> true

>> Person.connected? #=> false
>> Person
>> Person.connected? #=> true

connected? is the method which returns whether or not RAILS has connected with the DATABASE in which the table related to this MODEL exists.
Initailly when the RAILS application loads (CONSOLE starts), it has no DB connections.
When u say Animal, it fetches the schema of animals table by connecting to Database A.
So, after that , if u say Animal.connect? , it checks whether or not RAILS has connected to the DB in which the table related to Aniaml(animals) exists.
As it has already connected in the above step, the answer is TRUE.

Same is the case with Person.

The point to observe is that, Before working on the Model, Rails executes “establish_connection” method.
If it is not specified in the model, it connects to d DB as specified in the current ENVIRONMENT, if its not already connected.

But if we want o connect to a different DB as in the case with Person model, we override this, by specifying the DATABASE NAME to be connected by specifying the environment name in the “establish_connection” as the 1’st line of the Class.

Hope this is useful.

Permalink Leave a Comment

rake migrate in production environment

May 27, 2009 at 9:57 am (tasks) (, , , )

This is the code in the command line to run the database migrations in Production environment, infact any defined environment in database.yml

rake db:migrate RAILS_ENV=”name_of_the_environment”
Keep in mind that, the file called name_of_the_environment.rb should already be available in config/environments.rb

This can be just a copy of the existing files like config/development.rb

Permalink Leave a Comment

twitter user tweets

May 13, 2009 at 6:02 pm (Twitter) (, , )

The following code describes the way we can fetch the tweets of the user.

I just came across this wonderful GEM to integrate TWITTER in rails.
This is called Twitter4r

Its API is available at API

Its such an easy gem to use. I thought i shall throw the steps needed for the most regular task needed….Getting the tweets of a particular user.

Install twitter4r.

require “twitter”

twitter = Twitter::Client.new(:login=>”login_name”,:password=>”password”)tweets = twitter.timeline_for(:user,:id=>”user_name_of_user_whose_tweets_you_need”)
The result is an array
The important attributes of this array-elements are:
@text – The text of this tweet
@user – The user who posted this tweet
@user.name – name of the user who posted this tweet.
@user.screen_name – screen name of the user who posted this tweet

Hope this is useful.

Permalink Leave a Comment

Next page »

Follow

Get every new post delivered to your Inbox.