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