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

February 19, 2009 at 4:32 pm 1 comment

“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/

,

January 29, 2009 at 6:56 pm Leave a comment

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.

,

January 8, 2009 at 5:51 pm Leave a comment

“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

, , , , ,

December 9, 2008 at 4:13 pm Leave a comment

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

, , ,

December 9, 2008 at 4:01 pm Leave a comment

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

November 26, 2008 at 8:34 pm Leave a comment

“The ruby-debug Commands”


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

For a list of all available commands and options, use the help command.

, , ,

November 12, 2008 at 3:53 pm Leave a comment

Older Posts


Recent Posts

Pages


Follow

Get every new post delivered to your Inbox.