Posts filed under 'Rails'

code optimization

if params[:a].nil?
  if params[:b].nil?
       "999"
  else
     params[:b]
  end
else
  params[:a]
end

the same can be written using ternary operator

@a1 = params[:a].nil? ? params[:b].nil? ? ‘999′ : params[:b] :params[:a]

But you know RoR is so powerful…..
@a1 = params[:a] ||= params[:b] ||= ‘999′
[Background support : Ashish S.]

Add comment June 18, 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

Ruby On Rails Hosting:Brightbox

ruby_bag.jpgrails_bag.jpg

Traditionally, Ruby On Rails developers have had difficulty taking applications from their development systems to deployment (difficulties not experienced developing with PHP, ASP, or Java). But Brightbox, a U.K.-based startup showing off its wares at Startup Camp in London recently, specializes in Rails hosting.The idea really came from Rails development that Brightbox’s Jeremy Jarvis was doing along with his partners. When they had these deployment struggles, they built their own solution and gained some experience that others started to notice. As they began helping other development shops, Jarvis realized there was a business there and started Brightbox, which creates an optimized, flexibile, and easy-to-use service. He started the business last June and it went live in September 2007. (They have very cool T-shirts, too.)

Now, it’s only available today in the U.K. and Europe, so if you’re based in the United States … consider moving.The dedicated hosting service uses Xen to virtualize machines so that his customers have the ultimate flexibility; they can grow their servers as demands dictate, or ratchet down if need be.

Jarvis said that there are tools like Capistrano out there that help developers deploy applications, but Brightbox essentially created its own Capistrano recipes. Developers can download these to their own system and with a couple simple commands get it running on the server.Jarvis said Brightbox spends a good bit of time optimizing its servers to make them easy to use and stable.

Customers range from smaller Ruby developers up to Web development agencies, with a good deal of customers running Web 2.0 and social networking sites. Customers can choose the configuration from a pricing matrix, starting at 256 MB of RAM with a modest amount of storage, going up to a 4 GB dual-CPU box.

Add comment March 24, 2008

RadRails goes 1.0

RadRails, part of the Aptana IDE, is now available in version 1.0. The popular Rails development tool has been around for some time, but this latest version adds some useful new features for Ruby, as well as Ruby on Rails developers.

Highlights:

  • Integrated Rails shell console
  • Default-install and config of Ruby interpreter, database and debugger
    (Everything you need to get started fast)
  • Code completion with type inferencing
  • Code assist for Ruby, CSS, JS, and HTML inside RHTML files
  • Type hierarchy view
  • Go to declaration
  • Call hierarchy
  • Full implementation of RDT (Eclipse’s Ruby Development Tools project)
  • Integrated Profiler (Pro Version Only)

RadRails Vs NetBeans Vs 3rdRail

http://www.aptana.com/rails#features

Add comment March 22, 2008

find(ActiveRecord::Base)

# returns the object for very first row or dierctly put the first row ID like 156789
find_first_as_object = Rating.find(:first)
#p find_first_as_object.ratee_identity_id #1287065605

find_first_as_object_using_idnum = Rating.find(1) # search for id =1

# returns as an array of object(one element array)
find_first_as_array = Rating.find([569384240])
# p find_first_as_array[0].ratee_identity_id #1287065605

find_first_with_condition = Rating.find(:first, :conditions => [”rating = 4?])
==================================

find_objects_with_selected_attributes = Rating.find(:all,:select => ‘rating as RR’)

my_distinct = Rating.find(:all, :select =>”distinct rating” )

find_first_with_condition_plus_order = Rating.find(:first,
:conditions => [”rating = 4?],
:order => “ratingdate DESC”)
===================================

find_all = Rating.find(:all) # returns an array of objects

find_all_by_limit = Rating.find(:all, :conditions => [”rating >= 3 “],:limit => 10)

find_all_by_group = Rating.find(:all,
:conditions => [”rating >= 3 “],
:group => “rating”)
===================================

# find_by_sql returns an ARRAY of objects
find_sql = Rating.find_by_sql(”select rating as goodRating from ratings where rating >=3?)

find_sql_using_var = Rating.find_by_sql(”select * from ratings where id = ?”, ratee_identity_id)
===================================

find_first_by_rating = Rating.find_by_rating(”3?)
# Rating.find(:first, conditions => “rating = 3”)

find_all_by_rating = Rating.find_all_by_rating(”3?)
# Rating.find(:all, conditions => “rating = 3”)

Add comment March 22, 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