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.]

June 18, 2008 at 11:09 am Leave a comment

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.]

June 13, 2008 at 8:40 pm 1 comment

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"

April 29, 2008 at 12:16 pm Leave a comment

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”)

March 22, 2008 at 6:21 am Leave a comment

@content_for_layout

If you create a template file in the app/views/layouts with the same name as a controller, all views used by that controller will use that layout by default.controller/store_controller.rb ::::: consists of method1 & method2def method1
@first_name = “Rajesh”
end

def method2
@last_name = “Kumar”
end

views/store :::::: consists two html method1.rhtml & method2.rhtml

method1.rhtml
<h2>My first name is :: <%= @first_name %> </h2>

method2.rhtml
<h2>My last name is :: <%= @last_name %> </h2>

views/layouts/store.rhtml ::::: same as controller’s name
<html>
<body>
<h4>This is the main template </h4>
<i> Rails automatically sets the variable @content_for_layout to the page specific content- the stuff generated by the view invoked.</i>
<div>
<%= @content_for_layout %>
</div>
</body>
</html>

http:// ………/store/method1
This is the main template
Rails automatically sets the variable @content_for_layout to the page specific content- the stuff generated by the view invoked.
My first name is :: Rajesh

http:// ………/store/method2
This is the main template
Rails automatically sets the variable @content_for_layout to the page specific content- the stuff generated by the view invoked.
My last name is :: Kumar

March 22, 2008 at 6:19 am Leave a comment


Categories

posts[:recent]

@@name = PRAYAS

This blog has more intention for my self reference@24×7. But if some search engine crawler directs you to my page, believe I may learn form your comments or feedback. Hence feel free to comment good or bad!
prayas2010@gmail.com

visitors[:since_Mar'08]

free web counter

Prayas@Nikon

A colored life

Chhweet Kidss

An independent world

More Photos

Feeds


Follow

Get every new post delivered to your Inbox.