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' ) %>

Here is one way to center a fixed-width/fixed-height div at the center of its container. This could be adapted to centering text, images, etc. within their containers. Essentially, we do a bit of arithmetic to get the fixed-sized element centered using absolute positioning and margins. Note that the parent container must have a position: relative property for this to work.

div { position: absolute; top: 50%; left: 50%; width: 400px; height: 300px; margin-top: -150px; /* 1/2 of your element height*/ margin-left: -200px; /* 1/2 of your element width */ }

Re-initializing a table sequence is quite normal and required in some cases. I had to do that recently. If the table has data and needs to be preserved, then temporarily you have to move the data, delete the data in original table and re-initialize the sequence and copy the original data again.

Use copy column names to reinitialize key sequence.

In current versions of PostgreSQL you can specify column names with the COPY command to copy in or out only specific columns. This can come in handy if you want to recalculate your generated keys.

Given table:

    CREATE TABLE table_name (
        ID SERIAL PRIMARY KEY,
        name TEXT,
        role TEXT
    );
  

You can copy out only the name and the role like this:

      \COPY table_name (name, role) to 'something.dat'
  

And then you will want to delete your original data.

      DELETE FROM table_name;
  

Then you can reset your sequence. Perhaps you found out later that you wanted to start the id field at 100 instead of 1. To restart your sequence and copy the data back in use:

      ALTER SEQUENCE table_id_seq RESTART 100;
      \COPY people_131 (name, role) from 'something.dat'
  

This technique can be used to copy in new data if the COPY formatted data does not have the generated key info in it. In that case, you would NOT alter the sequence or delete data from your original table.

You can achieve the same effect of the original example by selecting the data into a temporary table instead of copying out. After you copy the data into the temporary table, delete the original data, reset the sequence and copy it back in.

      CREATE TEMPORARY TABLE people_temp AS SELECT name, role FROM people_131;
      ALTER SEQUENCE people_131_id_seq RESTART 1000;
      DELETE FROM people_131;
      INSERT INTO people_131 (name, role) select name, role from people_temp;
  

Things to notice:

  • * The sequence name (if you need it) is table_column_seq.
  • * The format of the data to copy in must match the columns speficied.