Posts Tagged Rails

Large development@Rails

With my recent Googling, I could crawl through few sites, blogs and data analytic graphs. I was shocked to know the jumping intercourse relation of web development with RoR, Specially on the blog http://www.railsonwave.com/ about the migration of site http://www.yellowpages.com/ from JAVA to Rails with so many positive results. I won’t make you to go through the blog and read each and every line unless you want…

Yes I am up with few highlights :

· Yellowpage has got 23 million visitors a month, this shows the Rails is fit for a large application in terms of performance.

· From 125.000 lines of code in Java, mainly written by external consultants from 2004 to 2005, to at least 20.000 lines of code in only 3 months of development from a team of 4 developers.

Sites those are developed in Rails and could be matter of a crown here, I would love to list few of them: (Yeah all of them has got 10+ million visitors@per month)

· http://www.yellowpages.com/

· http://www.revolutionhealth.com/

· http://www.43things.com/

· http://odeo.com/

· http://twitter.com/

Few more can be highlighted here:

· http://www.spock.com/

· http://penny-arcade.com/

· http://chowhound.com/

Add comment August 6, 2008

Generate Unique ID

Here is a very simple way to generate a unique id.
[Background support : Abhishek S.]

def generate_unique_id( len )
    chars_pattern = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
    unique_id = ""
    1.upto(len) {
			|i| unique_id << chars_pattern[rand(chars_pattern.size-1)]  }
    return unique_id
end
#generates a key of length 10
mykey = generate_unique_id(10)
print mykey # gives a result like "qeKX0myIQh"

Add comment June 23, 2008

render in rails

What do you see in a browser is nothing but a response; which is a combination of header and some document data.

From where does it come? -> Through the action of some controller.

Each action results in a response,

response = headers + document content

This resonse object is generated using various types of renders/redirects. Action Controller sends content to the user by using these rendering methods.

By default, actions are rendered within the current layout (if one exists)

  • AUTOMATIC RENDER

def myname
@name = “rajesh”
end

myname.rhtml ->My name is “#{@name}”

Yes this is also a type of render; an automatic rendering using instance var.

  • SIMPLE RENDER

def method1
@val = 30
render :action => “method2″, :layout => “mylayout1″
return
end

> This will execute the view of method2,

> Nothing will appear as a view for method1

> there is no relation what all defined in action def method2

> Any instance var defined before this statment can be executed in the view for method2 (not in action)
means in readered view of method2.rhtml
puts @val # returns you 30
where as
def method2
puts @val # dont expecct 30 in direct way
end

> TWO render is not possible in same action, else gives error, coz control exececutes code after the render statement.

  • RENDER PARTIAL

def method1
@val = 30
render :partial => “my_first_partial”
return
end

> render partial can be used in both controller as well as view, while plain render can be used in controller only
> In controller if you are using -> render :partial => “my_first_partial”
Layout will be lost of the current method i.e method1; But if you want to preserve it use layout as true
render :partial => “my_first_partial”, :layout => ‘true’
> If you are using it in view then layout of method1 will be preserved.

> instance var is well accessible in partial file, but if you want to pass the local var, then must use locals
render :partial => “my_first_partial” , :locals => {:q => 90}

  • RENDERING TEXT

render :text => “hello”, :layout => true
render :inline => “<%= ‘hello, ‘ * 3 + ‘again’ %>”, :layout => true

  • REDIRECTS_TO

redirect_to refers the method while render seeks you to the corresponding view.
You cant have these two together without any conditional statment.

RENDER -> populates a VIEW

REDIRECT -> hammers an ACTION

[Background support : Himanshu P.]

1 comment June 13, 2008

Overriding Active Record

1. If one wishes the table name should be singular instead of plural
Set the below configuration

ActiveRecord::Base.pluralize_table_names = false

2. If you want your model’s classname should represent a table of someother name

class Name < ActiveRecord::Base
   # by defualt it refers to "names" table in DB
   # Now let it refer to "nicknames"
   set_table_name "nickname"
end

3. If your primary key column is not named simply “id”, you can override this from within the class definition as well:

class Name < ActiveRecord::Base
     set_primary_key "nameid"
end

so when you want to save a record in DB (you must still use the id attribute to do so)

name_obj = Name.new
name_obj.id = 'N999'   #use the same   format this is eqivaled in saying name_obj.nameid='N999'
name_obj.save

but while accessing such record

print name_obj.nameid  # gives a result of "N999"

Add comment April 29, 2008

RailsConf 2008

May 29-June 1, 2008
Portland, Oregon

rails_conf.JPG


RailsConf is the official event for the growing Rails community.

Add comment April 24, 2008

Exception Handling in Ruby

>begin expression executes its body and returns the value of the last evaluated expression.
>Any error in begin part will be caught by rescue depending upon parameters
>ensure is the one which must be exectued irrespective of exception occured or not
>An error message caught by an exception can be accessed using $!

For Java Programmers [ begin => try ; rescue => catch; ensure => finally]

begin
 p "I am doing well"
 p "so well .. and well"
 a = 8/0
rescue
 p "Something went wrong => " + $!
ensure
 p "Oh Somehow I could finish my work"
end

O/P:

“I am doing well”
“so well .. and well”
“Something went wrong => divided by 0″
“Oh Somehow I could finish my work”

a = 8
b = 0
begin
 p "I am doing well"
 p "so well .. and well"
 if a==18
    p "I am happy with a as 8"
 elsif b == 0
    p "Lets say I dont want this"
    raise Exception
 else
    raise
 end

rescue
    p "Exception 1 caught here " + $!
rescue Exception
    p "Exception 2 caught here " +$!
ensure
 p "Oh Somehow I could finish my work"
end

O/P for [[ a = 8 and b = 0 ]]
“I am doing well”
“so well .. and well”
“Lets say I dont want this”
“Exception 2 caught here Exception”
“Oh Somehow I could finish my work”

O/P for [[ a = 8 and b = 1 ]]
“I am doing well”
“so well .. and well”
“Exception 1 caught here “
“Oh Somehow I could finish my work”

Add comment April 18, 2008

Previous Posts


Categories

posts[:recent]

episodes[:recycled]

@@name = PRAYAS

Step down at my blog with your ideas,comments,suggestions on Ruby,RoR,Ajax or Web2.0.You may reach me at
infostall@gmail.com

find_by_tags

1st April 37signals 360 2008 aamir aamir new look abdul abhinay access active active record aishwarya AJAX AMC amul and bollywood class facebook ghajini google hosting India instance local mozilla myspace nil NY object objects olympic plugin prayas programming Rails RoR Ruby security talent variables web web2.0 windows yahoo

Blogroll

visitors[:since_Mar'08]

free web counter

Spam Blocked

Feeds

Meta

RSS Prayas here