Tuesday, 10 September 2013

How to use has_many in rails view

How to use has_many in rails view


Here is the index_controller

  def index
    @categories = Category.all
  end

Here is the two models

1. Category model

has_many :sub_categories

2. sub_category model

belongs_to :category

And your view should contain

<% @categories.each do |category| %>
    <h1><%= category.title %></h1>
    <% category.sub_categories.each do |sub_category| %>
        <%= sub_category.descriptino %>
    <% end %>
<% end %>

Tuesday, 3 September 2013

phppgadmin configuration for rails

phpPgAdmin Configuration for rails


1. cd /etc/postgresql/9.1/main
2. sudo nano pg_hba.conf
3. Change the line from
    local        all        postgres               peer

to

    local        all        postgres               md5

4.  save and exit
5. Then open sudo nano postgresql.conf
6. Change the listen address as listen_address = '*'

7. Now try localhost/phppgadmin 

Sunday, 1 September 2013

get current week of the month and year

Get current week of the month and year


Get current week of the current year

select extract(week from date_trunc('month', current_date))

Get current week of the current month


select extract(week from current_date) - extract(week from date_trunc('month',current_date))+1

get last month last and first date

To get last month's last and first date


SELECT (date_trunc('month', now()-interval '6 month')::date - 1) as last_month_date

SELECT (date_trunc('month', now()-interval '6 month')::date ) as first_month_date 

The above query can be used to get last month's last date from current month

Thursday, 30 May 2013

Android view output in logcat

How to view android output in logcat:


Log.i("ref_name", String.valueOf(jArray.length()) );

Tuesday, 28 May 2013

Limit total words to be displayed - PHP

How to limit total words to be display in php



<?php
function get_words($text, $len){
$array = explode(" ", $text, $len+1);
if(count($array)>$len){
unset($array[$len]);
}
return implode(" ", $array);
}

$text = "This is a testing for limit word display.";

echo get_words($text, 3);
?>

Monday, 27 May 2013

Get tweets from twitter

How to get tweets in twitter


<?php
function get_tweet($username, $numberTweets)
{
$output = "";
$displayed = 0;
$url = "http://search.twitter.com/search.atom?q=from:$username&rpp=$numberTweets";
$xml = simplexml_load_file($url);
return $xml;
}
$tweet = get_tweet('your_name', 5);
foreach($tweet as $tweets){
echo $tweets->content.'<br />';
}
?>