Pages

Wednesday, May 28, 2014

Counters for Partials

Sometimes I like to number rows using a counter when rendering lists of things:
<% @products.each_with_index do |product, i| %>
  <li class="product_<%= i %>"><%= product.name %></li>
<% end %>
They are handy for testing, among other things.
Of course, if the content of the block gets too big I’ll factor it out into a partial. The idiomatic Rails way of doing this is to factor the block contents out into a partial and then render it with the :collectionoption:
< %= render :partial => "product", :collection => @products %>
Which is very clean, except now where do we get our row counter? As it turns out, Rails provides one for us:
  <li class="product_<%= product_counter %>"><%= product.name %></li>
Note the use of product_counter. Rails takes the name of the partial and adds _counter to it when providing the built-in counter.

Thursday, May 15, 2014

Copy a file from your system to EC2 Server with port and pem file



Here is command for Copying a file from your system to EC2 Server with port and pem file:

scp -P <PORT> -i xyz.pem  /home/user/file.txt  ec2-user@host:/home/ec2-user/

Below are the all available options that you can use based on your requirements:

scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2

Monday, May 12, 2014

Irreversible Migrations in Ruby on Rails

In Rails 2 in migrations there were only 2 methods: up and down. They are called on running migrations up and down respectively with rake tasks rake db:migrate and rake db:rollback.

In Rails 2

def up
add_column :users, :active, :boolean User.update_all(active: true) end

def down remove_column :users, :active end

Rails 3 produced us great method change which allowed us to write there code which creates a table and drops the table automatically on rolling migration back. But unfortunately there was a problem – you didn’t have opportunity to say “don’t run this peace of code on down but run this only on up”. So the solution was just to use old syntax. 

Thursday, May 1, 2014

Ruby remove duplicate entries in array of hashes but based on more than one value

Suppose you have the following array of hashes:

new_tracks = [{:name=>"Yes, Yes, Yes", :artist=>"Some Dude", :composer=> 'First Dude', :duration=>"3:21"},
 {:name=>"Chick on the Side", :artist=>"Another Dude", :duration=>"3:20"},
 {:name=>"Luv Is", :duration=>"3:13"},
 {:name=>"Yes, Yes, Yes", :artist=>"Some Dude", :composer=> 'First Dude', :duration=>"2"},
 {:name=>"Chick on the Side", :artist=>"Another Dude"}]

uniq accepts a block. If a block is given, it will use the return value of the block for comparison.
You can join the attributes you want into a string and return that string.

new_tracks.uniq { |track| [track[:name], track[:artist]].join(":") }

The resultant of above code will be an array of hashes unique on the basis of name and artist attributes.