Tuesday, December 23, 2008

NET HELPMSG

To provide help with a Windows error message (Typically the 4 digit number from the Event Viewer system log)


Type the following in a command prompt

NET HELPMSG message#
e.g.
NET HELPMSG 2184

Tuesday, December 16, 2008

Rare issue while deleting full text catalog in SQL server 2005

select * from sysfulltextcatalogs

This Lists all the full text catalogs present for the selected table. This table contains the catalog name and the path where these are physically stored.

In case of broken index or to delete a catalog after restoring database form an external source, update the path to a physical drive present in server. ( These steps to be followed only when the there is mismatch of catalog path in the new server or the SUBST command has failed )

update dbo.sysfulltextcatalogs set path='D:\Full Text Catalog' where ftcatid=X

by default update on sysfulltextcatalogs table is disabled, run the following script to enable this.

exec sp_configure 'allow updates',1
go
reconfigure with override

Wednesday, December 3, 2008

Rails logger article

info on logger :

http://dev.ctor.org/doc/devel-logger/classes/Logger.html

Use of concat tag

This example shows how to use concat tags in helper class, also note the use of rendering partial template inside this.


def content_headers(header = '', sub_header = '', options = {}, &block)
if !options.has_key?(:if) || options[:if]
content_body = capture(&block)
concat(render(:partial => 'shared/content_headers',
:locals => { :body => content_body, :header => header, :sub_header => sub_header }),
block.binding)
end

end

Monday, September 22, 2008

Three ways to invoke a method in ruby

1. object = Object.new
puts object.object_id
#=> 282660

2. puts object.send(:object_id)
#=> 282660

3. puts object.method(:object_id).call
#=> 282660

why in ruby nil.object_id is equal to 4 ?

A very good explanation found at http://www.joesniff.co.uk/ruby/rubyrails-interview-questions.html

The object_id method returns the identity of an object. Every object must have a unique id. Hence if two objects have the same object_id, they are the same object.

Ruby’s booleans and nil are objects, so they deserve to be treated like real objects just like the rest. So they too should have an object_id.

When allocating object ids we are restricted with what ids we can use as Fixnum uses the odd object_ids (well all those needed to reach the maximum Fixnum). It takes the odd ids as the least significant bit is always set to 1 to flag this as a Fixnum.

So starting at the sensible point of 0 this leaves us with the following order:

0 => False
1 => 0 (Fixnum)
2 => True
3 => 1 (Fixnum)
4 => Nil
5 => 2 (Fixnum)
6 => Undefined

Thursday, July 17, 2008

Wednesday, July 16, 2008

Non-English Chars in query string

Non-English chars when passed through query string can not be rebuilt to their orginal format unless some sort of encoding / decoding is applied to the chars.

In my project I had to receive Danish chars sent through query string and store them in database, the problem while saving the data was the Danish chars were either substituted by special chars (like ? or %) or were totally ignored.

After bit of R&D it was found that the query strings can be rebuilt by proper char encoding conversions. I used Ruby's Iconv library to achieve this.

An inside look

Usually we set SQL server in proper language mode. Like for danish support you would require

Danish_Norwegian_CI_AS mode enabled.


But rails by default is not equipped to differentiate lang. support. Usually rails is in UTF-8 mode.

Scenario : We need to compare for uniqueness of a user name column in users table which can contain Danish chars also. ( This is just an example as this validation can be done at Model level itself )

My comparison for existing record failed by using Find method

User.find_by_name('Bålåji')

Problem : Rails converted the string (
Bålåji) to UTF-8 mode forming a new string ( substituting å with some special char) thereby causing my Find method to return blank value projecting as if the record does not exist in the database and we can proceed with creating a new user.

Solution : Inorder to avoid such issue, I deliberately converted the input string to ISO-8859 format so as to preserver its original mode.

This can be done by

Iconv.conv('ISO-8859-1//IGNORE', 'UTF-8', STRING)


This converts a string from UTF-8 encoding to ISO-8859-1 encoding.

Now, when I try User.find_by_name('Bålåji') it returns me the correct match.

This conversions can be used in cases like

> importing values for a txt file / CSV .

> Comparing values in ruby standalone script.


Sunday, May 4, 2008

IP based restriction in Apache

We can restrict the access to any particular URL and its sub urls by modifying apache's Config. file

For example : if we have to restrict admin section of our site to be browsable only by few specified IP then in apache's Config. file we just have to add

<\Location /admin>
Order Allow,Deny
Allow from 191.16.25.62
<\/Location>
( Please remove the '\' before open and close Location tag's )


This is only allow requests coming from 191.16.25.62 , requests from any other IP will get

Forbidden

You don't have permission to access /admin on this server.

This restriction also applies to the sub-urls like admin/user_list etc....

Suppose we have to provide access to more than one IP, then we just have to provide the different IP's separated by a space between them.

Like

Allow from 191.16.25.62 191.16.25.63 ..........

The same can be applied for rejecting a request from a specified IP


Order Allow,Deny
Deny from 191.16.25.62

Monday, April 14, 2008

Increment/decrement operator's in Ruby

Ruby has no pre/post increment/decrement operator. For instance, x++ or x-- will fail to parse. More importantly, ++x or --x will do nothing! In fact, they behave as multiple unary prefix operators: -x == ---x == -----x == ...... To increment a number, simply write x += 1.

Methods in a class

To find the pre defined methods defined for a class type

ri classname
in your command prompt

Example --- ri File ; ri String

Friday, April 11, 2008

Articles on Ruby's GC

http://whytheluckystiff.net/articles/theFullyUpturnedBin.html

Use params, not @params

Frequently people in the #rubyonrails channel use @params in their code. For a while now @params has been deprecated in favor of simply params. For those who just skim these blog posts:
Use params, not @params

Why? When you use the params method, it allows for the implementation details of the parameter hash to be changed without breaking existing code. If the implementation of params changed you wouldn’t have to change your code at all because the single point of access for the parameters would just remain the params method. So, the details of what is happening behind the scenes don’t matter. If, though, you use the @params instance variable directly, you’ve broken encapsulation and consequently the ability for the implementation to be easily modified. Methods can be refactored, but instance variables can’t. Today the params method just wraps the @params instance variable, so still using @params works, but that’s not guaranteed to always remain the case.

Same goes for request, response, session, headers, template, cookies and flash.

Basically, a good rule of thumb here is don’t use an instance variable in your controller or view unless you created that instance variable.

Even the old @content_for_layout in the layout is deprecated in favor of just using yield in its place. Also content_for('some_fragment') is now accessed with yield :some_fragment rather than @content_for_some_fragment.

Shortcut icons for web

After creating the shortcut icon for your webpage, you must associate it with your Web page. There are two methods for doing this.

The first method is to save the icon with the default file name of favicon.ico to the root directory of your domain—for example, www.microsoft.com/favicon.ico. The first time a user visits your Web page, Internet Explorer automatically searches for this file and places the icon in the address bar, next to all favorites linking to your site, and on page tabs. In Internet Explorer 5 and Internet Explorer 6, the icon will appears only after a user adds the site to the Favorites menu.

The second method for associating a shortcut icon with your Web page is to add a line of HTML code to the page's head element. The line of code includes a link tag that specifies the location and name of the icon file. You can include this link tag on a per-page basis. First, save the icon with a file name other than favicon.ico, and then add the following code to the head element of your page.

<\link rel="SHORTCUT ICON" href="http://www.mydomain.com/myicon.ico">


You can use either method, or both. However, if you use the second method, whichever icon you point to in the link tag on each page will be displayed instead of the default favicon.ico file at the root of your domain.