Select Random Records

Posted on February 18, 2009, under MySQL, Rails.

I wanted for some reason to have random records from a model without any condition, just random records and here’s how you have to do it.

1
<% User.find(:all, :order => 'RAND()', :limit => 3) %>

Hope this helps somebody!

Cheers!!!

Multiple Unfuddle Accounts

Posted on February 17, 2009, under Ubuntu.

I was having my own free unfuddle account and my work also had another unfuddle account. Now I had configured my public key for my work account and when I updated the same public key for my personal account, it didn’t accept it.

It throwed a error already this key is in use.

So, what the hell do I have to do? Here’s what I did to solve it….Simple steps though

STEP 1: Create another public key

1
ssh-keygen -t rsa -f personal_unfuddle

As you know it will generate a key and store it file name “personal_unfuddle” -f parameter is a optional parameter and it will store it in that file.

Now you have to view the generated file to update the public key in the unfuddle

1
cat personal_unfuddle.pub

Copy paste the key into your unfuddle account.

STEP 2:
Identifying the Key pair for the multiple account.

Create/Edit a file named “config” in youe .ssh folder

1
sudo vim config

Paste the following code into the config file


Host work.unfuddle.com
User git
IdentityFile ~/.ssh/id_rsa

Host personal.unfuddle.com
User git
IdentityFile ~/.ssh/personal_unfuddle

After few mins, this should start working.

Hope this helps someone

Photo Gallery up in seconds

Posted on February 16, 2009, under Miscellaneous.

Hey, recently I was looking for a way to display the picasa web albums on my blog and discovered a really cool plugin, with so sweat.

http://wordpress.org/extend/plugins/kpicasa-gallery/installation/

This was really a very cool plugin and its so easy to get it done in fraction of seconds…..Way too fast.

Also you can use specify the Albums that you wanted to display. For instances, you may wanted to show certain albums and not all.

KPICASA_GALLERY(album_name, album_name2, album_name3)

Really cool ah!

Rails Default Layout

Posted on February 15, 2009, under Rails.

As we all know, for any controller, by default rails looks the controller name .rthml files and if it doesn’t exists then it looks application.rhtml.

Since my site is very simple, I don’t have many layouts and just one layout and so i stored the layout file in app/views/layouts/application.html.erb and i removed all other layout files so that by default it will read application.html.erb.

Hope this helps somebody!

Pads

Load Fixtures into the Database

Posted on February 12, 2009, under Rails, Shell Scripts.

I was looking to load the fixtures into my database and it is very simple. Please look at the following code

1
rake spec:db:fixtures:load

Hope this helps somebody.

Cheers!

Pads

Migartion in Different Envirnoments

Posted on February 10, 2009, under Rails.

Wanna run db:migrate command in other environments rather than development, then it is how so:

Test Environment

1
rake db:migrate RAILS_ENV=test

Production Environment

1
rake db:migrate RAILS_ENV=production

Obviously, these commands has to be executed from the root path of the application.

See ya

Hacking a nil object / Workaround

Posted on February 6, 2009, under Rails.

I was looking for a way to workaround a nil object. For instance if @project.name is nil, I wanted to display as “N/A” and otherwise the project name (@project.name)

Normal Way

1
2
3
4
5
<% unless @project.nil? %>
  <%=  @project.name %>
<% else %>
  <%=  "N/A" %>
<% end  %>

Another Way

1
<%= @project.name rescue nil || "N/A" %>

Just in a one single line!, Neat ah :-)

Hope this helps!

Reset button in Rails

Posted on February 4, 2009, under Rails.

I was looking for a way to have a reset button in the rails way, instead of the direct HTML tag.

1
<%= submit_tag "Start over", :name => ‘reset’, :type => ‘reset’, :id => ‘task_reset’ %>

Rmagick on Ubuntu

Posted on January 21, 2009, under Rails, Shell Scripts, Ubuntu.

Hey, Rmagick is a big pain installing it. I was really lazy and tired to install it. Previously in windows version, you need to do lot of configurations for it and it was really time consuming one and also hectic.

But to my surprise installing it in Ubuntu (8.10) was really really a breeze and was so cool and here’s the 2 gracious command to install Rmagick in Ubuntu.

1
2
3
sudo apt-get install libmagick9-dev ruby1.8-dev
 
sudo gem install rmagick

Using a partial form with form_for

Posted on January 20, 2009, under Rails.

I just wanted to use partial form for CRUD using Rails 2.2.2 and when i had created a form i got the following error:

undefined local variable or method `f’ for #

Old Code

1
2
3
4
<% form_for(@project) do |f| %>
 <%= f.error_messages %>
<%= render :partial => 'form' %> 
<% end %>

New Code

1
2
3
4
<% form_for(@project) do |f| %>
  <%= f.error_messages %>
<%= render :partial => 'form', :locals => { :f => f } %>    
<% end %>