theKindOfMe

January 26, 2012

Rails Assets Pipeline Survival Guide

Filed under: Uncategorized — Tags: , , , , — yasi8h @ 1:50 am

Helpful settings

If you have assets pipeline enabled. By default you need to precompile your assets every time you change something (in production env). Having everything precompiled is cool but this will ask rails compile any assets that are not already compiled.

# Don't fallback to assets pipeline if a precompiled asset is missed
 config.assets.compile = true

Page Specific JS/CSS

If you want some JS/CSS to only be included in a specific set of pages.

  • break the page specific JS/CSS code to different files (ex:- profiles.js)
  • make sure they are not referenced in your manifest file (application.js)
  • add the following code snippets to the necessary files
application.html.erb (your main template)
<head>
 <title>FamousApp</title>
 <%= stylesheet_link_tag "application" %>
 <%= javascript_include_tag "application" %>
 <%= yield :head %>
 <%= csrf_meta_tags %>
</head>
specific_page.html.erb (the template where you want your JS to appear)
<% content_for :head do %>
 <%= javascript_include_tag "profiles" %>
<% end %>

production.rb

# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
 config.assets.precompile += %w( profiles.js )

Deploying with Capistrano

This is easy enough

load 'deploy/assets'

If you ever run across this problem:

# Disable warning message about missing dirs 'public/[javascripts|images|stylesheets]'
# src: https://github.com/capistrano/capistrano/issues/79
set :normalize_asset_timestamps, false

January 17, 2012

Note: Test and View available Rails Routes

Filed under: Uncategorized — Tags: , , — yasi8h @ 3:39 am

See all available routes

rake routes

Check route/path/URL helpers (in rails console)

app.the_route_you_want_to_test_path

OR

app.the_route_you_want_to_test_url

January 5, 2012

SQL ‘Group By’ + ‘Order By’ in Mongodb

Filed under: Uncategorized — Tags: , — yasi8h @ 4:50 am

As a newbie to mongodb i was a bit puzzled when i couldn’t find a equivalent to the SQL group by combined with order by in mongodb.

Group

If you just want to go a SQL group by in mongodb you can use the group() function (refer http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Group). however the results you get from group function cannot be sorted using the sort() function. This is because group function does not return a cursor (on which you can sort(), limit()…etc), but a single document.

You can’t sort the document wich is returned by group but you can sort the document in the client side. This is one way to go about doing a ‘group by + order by’. However if you don’t want to sort things in the client side you can use mapreduce.

MapReduce

This is probably not as simple as you would want it to be. But until mongodb supports a one liner for group by + order by function this is the only way to go about it.

You can refer the MapReduce documentation in the official site to get a better understanding about how it works. But if you just want to do a simple group by + order by equivalent in mongodb you can look at the sample bellow.

Mongodb Data Structure
gplus_profiles: id(objectId), have_user_in_circles_count (int)
SQL
SELECT COUNT(id), `have_user_in_circles_count` from `gplus_profiles`GROUP BY `have_user_in_circles_count` ORDER BY 1 DESC
Mongodb
m = function(){
emit(this.have_user_in_circles_count, { count : 1 });
};
r = function(key, values) {
var total = 0;
for ( var i=0; i<values.length; i++ )
total += values[i].count;
return { count:total };
};
res = db.gplus_profiles.mapReduce(m, r, { out : "myoutputt" } );
db.myoutputt.find().sort({value:-1})

Although we have to use mapreduce for the time being, a later version of mongodb will support group by + order by like functionality natively.


	

Blog at WordPress.com.