Sunday, September 5, 2010

load vs require

Load

To load a file we use the load method:

load ‘blah.rb’

Note that we must supply the extension when we use it. When Ruby encounters a load, it will read in the contents of the file you’re trying to load. It will do this every time. No matter how many times you load the same feature, Ruby will read the file in every time it encounters a load. You’re not limited to just supplying a name and extension though, you can navigate directories e.g.:

load ‘../../blah.rb’

or even give an absolute path:

load ‘/a/b/c/blah.rb’


Require

No matter how many times you require the same feature in your program, only the first time is significant. Ruby will not re-read a file a second time, this is the first fundamental difference from how load works. The other obvious difference is the fact that you don’t need to supply an extension when you require a feature:

require ‘blah’

Friday, August 27, 2010

Ruby: Dynamically Define Method

http://blog.jayfields.com/2008/02/ruby-dynamically-define-method.html

Thursday, July 29, 2010

Wednesday, July 21, 2010

Search BY first name or last name or canacatination of first name and last name

named_scope :username_like, lambda { |value| {:conditions => ["first_name LIKE ? OR last_name LIKE ? OR CONCAT(first_name,' ',last_name) LIKE ?",value,value,value]} }

Friday, June 25, 2010

Wednesday, June 16, 2010

Select/Un Select check boxes with javascript

view:

<%=check_box_tag :id => "select_all" %>

User List
<% @users.each do |user| %>
<%=check_box_tag "recipient[]", :value => "user.email" %>
<% end %>


javascript code:

script type ="text/javascript">
$(function(){
$("#select_all").click(function(){
$flag = $(this).attr('checked');
if ($flag) {
$("input[name='recipient[]']").each(function(){
$(this).attr('checked', true)
});
}else{
$("input[name='recipient[]']").each(function(){
$(this).attr('checked', false)
});
}
});
});

Rails dynamic layout selection

controller:
class MyController < ApplicationController
layout :check_layout




private

def check_layout
return current_user.admin? ? "admin" : "application"
# current_user is user who logged in
end


end