I have done some research about improving Rails Applications performance. I would feel the the following few points need to be considered when we optimize rails applications for performance:
1) Avoid the use of dynamic URL generation (link_to, url_for) since rails needs to look up the routes table and that may take time. Just hard code the controller name and the action.
2) Try to avoid the excess use of helpers since it adds overhead.
3) You may consider to use Rails Bench to do some testing for you rails application performance.
4) You may consider to use memcached to cache your model and library computation results.
5) try to optimize your database queries. If you use ActiveRecord find, be careful from computation intensive sql queries that returns to you a lot of data that you may not need. The method find may run many select statements for you.
Working with Ruby on Rails since july 2007. RoR web application development along with other technologies like LAMP.The reason to love programming with RoR is, it helps keep code simple, clean and nice… Also working with php/cakephp etc.
Saturday, January 24, 2009
Wednesday, September 24, 2008
observe_field Example - Ruby on Rails
<table>
<tr>
<td><label for="searchtext"><font size="1"><b>Live Book Search:</b></font></label></td>
<td><%= text_field_tag :searchtext %></td>
<td><img alt="spinner" id="spinner" src="/images/spinner.gif" style="display:none;" /></td>
</tr>
</table>
<%= observe_field 'searchtext', :frequency => 0.5,
:update => 'search_hits',
:loading => "Element.show('spinner')",
:complete => "Element.hide('spinner')",
:url => { :controller => 'books', :action=> 'live_search' },
:with => "'search=' + escape(value)" %>
<div id="search_hits">
<table>
<tr>
<th>Name</th>
<th>Author</th>
<th>Price</th>
<th>Desc</th>
</tr>
<% for book in @books %>
<tr>
<td><%=h book.name %></td>
<td><%=h book.author %></td>
<td><%=h book.price %></td>
<td><%=h book.desc %></td>
<td><%= link_to 'Show', book %></td>
<td><%= link_to 'Edit', edit_book_path(book) %></td>
<td><%= link_to 'Destroy', book, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
For more detail go to
Wiki.rubyonrails.org
<tr>
<td><label for="searchtext"><font size="1"><b>Live Book Search:</b></font></label></td>
<td><%= text_field_tag :searchtext %></td>
<td><img alt="spinner" id="spinner" src="/images/spinner.gif" style="display:none;" /></td>
</tr>
</table>
<%= observe_field 'searchtext', :frequency => 0.5,
:update => 'search_hits',
:loading => "Element.show('spinner')",
:complete => "Element.hide('spinner')",
:url => { :controller => 'books', :action=> 'live_search' },
:with => "'search=' + escape(value)" %>
<div id="search_hits">
<table>
<tr>
<th>Name</th>
<th>Author</th>
<th>Price</th>
<th>Desc</th>
</tr>
<% for book in @books %>
<tr>
<td><%=h book.name %></td>
<td><%=h book.author %></td>
<td><%=h book.price %></td>
<td><%=h book.desc %></td>
<td><%= link_to 'Show', book %></td>
<td><%= link_to 'Edit', edit_book_path(book) %></td>
<td><%= link_to 'Destroy', book, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
For more detail go to
Wiki.rubyonrails.org
Variables In Request Env - Ruby on Rails
request.env is a Ruby Array that contains information about a visiting user’s and server environments.request.env is accessible on all pages hosted in a ruby on rails site. The Array contains the following key/value pairs:- SERVER_NAME
- PATH_INFO
- REMOTE_HOST
- HTTP_ACCEPT_ENCODING
- HTTP_USER_AGENT
- SERVER_PROTOCOL
- HTTP_CACHE_CONTROL
- HTTP_ACCEPT_LANGUAGE
- HTTP_HOST
- REMOTE_ADDR
- SERVER_SOFTWARE
- HTTP_KEEP_ALIVE
- HTTP_REFERER
- HTTP_COOKIE
- HTTP_ACCEPT_CHARSET
- REQUEST_URI
- SERVER_PORT
- GATEWAY_INTERFACE
- QUERY_STRING
- REMOTE_USER
- HTTP_ACCEPT
- REQUEST_METHOD
- HTTP_CONNECTION
request.env information in it.<table>
<th>key</th>
<th>Value</th>
<% for item in request.env %>
<tr>
<td><%= item[0] %></td>
<td><%= item[1] %></td>
</tr>
<% end %>
</table>
Installing Rmagick on Windows Using Ruby 1.8.6
Rmagick tends to be tricky to install, but on windows its relatively simple, except for the fact that if you freshly installed ruby 1.8.6, ruby gems needs to be updated. If you install ruby 1.8.6 and then go to ruby.forge and download rmagick for windows. Extract the zip folder, run the .exe, then open a command line, cd (change directory) to the install folder and do “gem install rmagick —local”. The first time you do this, you might run into the “Error installing gem RMagick (version) .gem[.gem]: buffer error”. To fix that run “gem update —system” with double hyphens and then run “gem install rmagick —local” and everything should now install fine.
Dynamically add and remove html elements using javascript
<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="addElement();">Add Some Elements</a></p>
<div id="myDiv"> </div>
<script>
function addElement() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
ni.appendChild(newdiv);
}
function removeElement(divNum) {
var d = document.getElementById('myDiv');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}
</script>
<p><a href="javascript:;" onclick="addElement();">Add Some Elements</a></p>
<div id="myDiv"> </div>
<script>
function addElement() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
ni.appendChild(newdiv);
}
function removeElement(divNum) {
var d = document.getElementById('myDiv');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}
</script>
Monday, August 25, 2008
NAMED_SCOPE in Ruby on Rails
The has_finder gem has been added to Rails with a different name: named_scope
For better understand let go through the following example:
class Article < ActiveRecord :: Base
named_scope :published, :conditions => {:published => true}
named_scope :containing_the_letter_c, :conditions => "author LIKE '%c%’"
end
Article.published.paginate(:page => 1)
Article.published.containing_the_letter_c.count
Article.containing_the_letter_c.find(:first)
Instead of creating a new method named published to return all published posts, I'm using a named_scope to do it for me.
But it can go even further than this. Let's look at another example of how it can be used:
named_scope :written_before, lambda { |time|
{ :conditions => ['written_on < ?', time] }
}
For better understand let go through the following example:
class Article < ActiveRecord :: Base
named_scope :published, :conditions => {:published => true}
named_scope :containing_the_letter_c, :conditions => "author LIKE '%c%’"
end
Article.published.paginate(:page => 1)
Article.published.containing_the_letter_c.count
Article.containing_the_letter_c.find(:first)
Instead of creating a new method named published to return all published posts, I'm using a named_scope to do it for me.
But it can go even further than this. Let's look at another example of how it can be used:
named_scope :written_before, lambda { |time|
{ :conditions => ['written_on < ?', time] }
}
Search from multiple tables in mysql
CREATE TABLE `tagsearch`.`books` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(200) NOT NULL,
`author` varchar(100) NOT NULL,
`price` float(10,2) NOT NULL,
`comment` text,
`created_on` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`updated_on` timestamp NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
)
CREATE TABLE `tagsearch`.`tags` (
`id` int(11) NOT NULL auto_increment,
`tagname` varchar(200) NOT NULL,
`counter` int(10) NOT NULL default '0',
`created_on` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`updated_on` timestamp NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
)
Suppose we have in database these two tables.... and search query is as below.....
select * from books,tags WHERE MATCH(author,comment,tagname) AGAINST ('rails' in boolean mode);
Here, keyword is search from two tables and Match includes the field name of different table and against have search keyword
`id` int(11) NOT NULL auto_increment,
`name` varchar(200) NOT NULL,
`author` varchar(100) NOT NULL,
`price` float(10,2) NOT NULL,
`comment` text,
`created_on` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`updated_on` timestamp NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
)
CREATE TABLE `tagsearch`.`tags` (
`id` int(11) NOT NULL auto_increment,
`tagname` varchar(200) NOT NULL,
`counter` int(10) NOT NULL default '0',
`created_on` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`updated_on` timestamp NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
)
Suppose we have in database these two tables.... and search query is as below.....
select * from books,tags WHERE MATCH(author,comment,tagname) AGAINST ('rails' in boolean mode);
Here, keyword is search from two tables and Match includes the field name of different table and against have search keyword
Subscribe to:
Posts (Atom)