FB Share Test via RelatedNoise.

Testing FB Sharing for greater good.

To retrive a random row from a table you have do just this:

 Model.find :first, :offset => ( Model.count * rand ).to_i 

This is fast and requires no custom SQL queries! All this does is count number of rows in a table and select one row at some offset while still having the table ordered by primary key.

It uses offset (limit), not ID number, so it chooses n-th found row, not a row with ID equal to n. So if you have IDs like 1, 2, 4, 8and get 3 at random, this method will take 3rd row, which has ID = 4.

You can recover MySQL database server password with following five easy steps.

Step # 1: Stop the MySQL server process.

 # sudo /etc/init.d/mysql stop 

Step # 2: Start the MySQL (mysqld) server/daemon process with the --skip-grant-tables option so that it will not prompt for password.

 # sudo mysqld_safe --skip-grant-tables & 

Step # 3: Connect to mysql server as the root user.

 # mysql -u root 

Step # 4: Setup new mysql root account password i.e. reset mysql password.

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

Step # 5: Exit and restart the MySQL server.

# sudo /etc/init.d/mysql stop

# sudo /etc/init.d/mysql start

# mysql -u root -p

You have just changed the password for the root user of mysql database.

It's very common to want to highlight the current link within a navigation list for the current action being performed. If you are using Ajax, then this is easy, handle the onclick event to highlight the current link.

<%= link_to 'Home', 'javascript:void(0);', 
  :onclick => "this.addClassName('youarehere');this.siblings().each(function(s){
  s.removeClassName('youarehere');});", :class => '', :id => 'home' %> 

<%= link_to 'News', 'javascript:void(0);', 
  :onclick => "this.addClassName('youarehere');this.siblings().each(function(s){
  s.removeClassName('youarehere');});", :class => '', :id => 'news' %> 

Make sure you have a CSS class called youarehere. The following CSS is just an example.

.youarehere {
    font-size: 11pt;
    text-decoration: underline;
}

If you are not using Ajax, you need to know what the current action and controller is. But how to know what the currect action is (without doing some ugly URL parsing)?

The first thing to do is add the following code to your application_helper.rb file

def section_link(name, options)
  action      = options[:action] || 'index'
  controller  = options[:controller] 
  
    if action == @current_action && controller == @current_controller
      link_to(name, options, :class => 'youarehere')
    else
        link_to(name, options)
    end
end

The above method always expects an :controller key to be passed in with the options hash. If you don't pass the optional :action key, it will assume 'index' as the action being performed.

We need to know what the current_controller and current_action is for the above method to work. Finally put this in your application_controller.rb file, as it makes the current executing action and controller name available to the helper.

before_filter :instantiate_controller_and_action_names

def instantiate_controller_and_action_names
    @current_action         = action_name
    @current_controller = controller_name
end

In your view you have to use:

<%= section_link( "Home", :controller => 'welcome', :action => 'index' ) %> |

<%= section_link( "News", :controller => 'news', :ref => 'home' ) %>

(function() { var links = document.getElementsByTagName('a'); var query = '?'; for(var i = 0; i < links.length; i++) { if(links[i].href.indexOf('#disqus_thread') >= 0) { query += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&'; } } document.write('