“Dynamic finders with hash attributes for creation”
Here’s a typical situation:
tag = Tag.find_by_name(name)
if tag.nil?
tag = Tag.create(:name => name, :creator => current_user)
end
You’d think the way to do that would be with the find_or_create_by_name dynamic finder, but that doesn’t work since there’s no way to search by name only, but create by name and creator. Well, that’s just not right. So here’s what we’ll do:
Tag.find_or_create_by_name(:name => name, :creator => current_user)
It’s not a big deal or something I use every day, but I like it anyway.
Source : google .

1 comment February 19, 2009
“Highlighting Text in Rails “
Presumably you’ve seen those Web 2.0 sites that highlight random words in the marketing text to draw your attention. A little-known view helper in Rails makes this sort of thing trivial. For example, you can combine this markup in your view:
view plain copy to clipboard print
1.
With this bit of CSS:
view plaincopy to clipboardprint
1. .highlight { background:#ff0; }
.highlight { background:#ff0; }
And you get this result:
highlight
Easy enough. But wait, there’s more! You can specify an entire array of words and phrases to highlight:
view plaincopy to clipboardprint
1.
Even better, highlight supports a :highlight option, which lets you specify a custom string to use for highlighting. The token \1 will be replaced with the text to be highlighted. This lets you change the HTML markup:
view plaincopy to clipboardprint
1. ‘\1‘) %>
‘\1‘) %>
This ability to insert arbitrary surrounding markup makes highlight more flexible, if you let yourself think out of the box:
view plaincopy to clipboardprint
1. ‘\1) %>
‘\1) %>
It’s worth taking a dip into ActionView::Helpers occasionally to see what other bits of functionality are lurking that you’ve forgotten about.
Source : http://afreshcup.com/2009/01/29/highlighting-text-in-rails/
Add comment January 29, 2009
Installing ActiveMQ
Step1 :
Download the 4.X.X from the below link
http://activemq.apache.org/getting-started.html#GettingStarted-Download
Step 2:
From a console window, change to the installation directory and run ActiveMQ:
cd [activemq_install_dir]
where activemq_install_dir is the directory in which ActiveMQ was installed, e.g., c:\Program Files\ActiveMQ-4.x.
Then type:
bin\activemq
NOTE: Working directories get created relative to the current directory. To create working directories in the proper place, ActiveMQ must be launched from its home/installation directory.
If unix/linux
From a command shell, change to the installation directory and run ActiveMQ:
cd [activemq_install_dir]
where activemq_install_dir is the directory in which ActiveMQ was installed, e.g., /usr/local/activemq-4.x.
Then type:
bin/activemq
OR
bin/activemq > /tmp/smlog 2>&1 &;
Note: /tmp/smlog may be changed to another file name.
Add comment January 8, 2009
“Sum/Avg method in Rails , save a query”
Ever wondered about the Calculations in Rails, I used to use the group_by in SQL or select to do many calculations while fetching the results , but sometimes Rails doesnt allow to use group etc , like when we are doing AJAX calls in v2.1.0 of rails .
Then Joejeet helped me to get the following calculations methods , it was so beautiful that my 3 line of code was convered to 1 single line.
Code
@rating_sum = Rating.sum(’rating’, :conditions =>["rateable_id = ? and rateable_type = ?", @rating.rateable_id, @rating.rateable_type])
There are other methods like Avg,minimum, max,count etc available under
Module: ActiveRecord::Calculations::ClassMethods
Add comment December 9, 2008
“Changing the class of a div using rails NOT JS”
Consider we need to change the class of a particular DIV/SPAN , I was wondering how we could have done using some default rails method and NOT Javascript as what we can do in Javascript the same we can do using Rails but in lesser time and number of lines of code.
Using google with the correct key words resulted in the following
Javascript
function change(id, newclass)
{
identity=document.getElementById(id);
identity.className=newclass;
}
Rails
page["task_#{params[:id]}”].toggle_class_name “completed”
Add comment December 9, 2008
“SQL inner joins the right way “
The right way
————————————-
select s.CompanyName
from Suppliers as s
where exists
(
select * from products p
inner join categories c on p.CategoryID = c.CategoryID
where c.CategoryName = “Seafood”
and p.SupplierID=s.SupplierID
)
—————————————-
The wrong way
————————————————
SELECT DISTINCT Suppliers.CompanyName
FROM Suppliers INNER JOIN (Categories INNER JOIN Products ON
Categories.CategoryID = Products.CategoryID) ON Suppliers.SupplierID =
Products.SupplierID
WHERE (((Categories.CategoryName)=”Seafood”))
————————————————
Add comment November 26, 2008
“The ruby-debug Commands”
What follows is a quick rundown of the most important ruby-debug commands, along with a brief description of what they do. Don’t worry too much about remembering every last detail — the built-in help command will list all the available commands for you. You can also use the For a list of all available commands and options, use the
The ruby-debug Commands
help <commandname> syntax to get help with a specific command.
backtrace: Display a trace of the execution stack, similar to what is displayed when your application raises an exception.break/delete: Display a list of breakpoints that have been set in your application. This command is also used to set new breakpoints, or delete existing ones, from within the ruby-debug shell.cont: Leave the current debugger shell and resume execution of the application until the next breakpoint is encountered.irb: Invoke an interactive Ruby interpreter at the current point of execution, similar to the shell used by the breakpoint library.list: Display the code fragments surrounding the current point of execution. (We’ll make use of this command in a moment.)method/method instance: Explore the available class methods and instance methods, respectively.next/step: Continue execution one step at a time — this is a huge improvement over the breakpoint library.p/pp: Short for print and pretty print respectively, these commands can be used to evaluate Ruby expressions and display the value of variables to the console.quit: Exit the debugger. Note that this will also exit the application server if it was invoked from the command line, as demonstrated above. To just exit the current debugging session, use cont.reload: Reload the Ruby source files from disk. This can be useful if you’ve changed class definitions and want to reload them dynamically without leaving the current debugging session.help command.
Add comment November 12, 2008
“Debugging in rails”
Well ever thought of debugging the rails application , there are many ways , one way which I liked of lately was the debugger in action directly.
Step 1 : Install ruby-debugger gem “sudo gem install ruby-debugger –include_dependencies”
Step 2 : Dont forget to start debugging server by saying
ruby script/server –debugger
Step 3 : Say debuggger in the action, the console will open in the command line for the code written below to debugger
eg:
def foo
debugger
@object = Active.find(:all)
end
When in that case in the command line we will get a rbd : opened , where you can debug the object like @object.size etc
Thats it enjoy , there are some other methods like <%= debug @object %> , <%= @object.inspect %> , puts etc which I guess all of us already know abt.
1 comment November 12, 2008
Download songs
If you are looking to download songs and are bored of using torrents and .in sites then try the below link.
Here you shall get direct downloads and that to for free, yes I said free ,wow …
Go ahead.Search and Download.
Add comment November 5, 2008
SQL and Active Record
find(:all, :conditions =>…) method call pos = Order.find(:all, That’s fine if your condition is predefined, but how do you handle the situation its better to use name = params[:name] OR we can also do in below fashion
:conditions => “name = ‘Dave’ and pay_type = ‘po’” )
The result will be an array of all the matching rows, each neatly wrapped in
an Order object. If no orders match the criteria, the array will be empty.
where the name of the customer is set externally (perhaps coming from a web
form)? One way is to substitute the value of that variable into the condition
string.
# get the limit amount from the form
name = params[:name]
# DON’T DO THIS!!!
pos = Order.find(:all,
:conditions => “name = ‘#{name}’ and pay_type = ‘po’” )
pos = Order.find(:all,
:conditions => ["name = ? and pay_type = 'po'" , name])
may be use placeholders
Each placeholder is of the form :name,and the corresponding values are supplied as a hash, where the keys correspond
to the names in the query.
name = params[:name]
pay_type = params[:pay_type]
pos = Order.find(:all,
:conditions => ["name = :name and pay_type = :pay_type" ,
{:pay_type => pay_type, :name => name}])
pos = Order.find(:all,
:conditions => ["name = :name and pay_type = :pay_type" , params[:order]])pos = Order.find(:all,
:conditions => ["name = :name and pay_type = :pay_type" , params[:order]])
as params is effectively a hash, you
can simply pass it all to the condition
Add comment November 4, 2008