Langsung ke konten utama

Postingan

Menampilkan postingan dari Mei, 2013

Rails PostgreSQL Group BY query

The blue/white elephant logo of the PostgreSQL RDBMS in SVG format. (Photo credit: Wikipedia) Hi Everyone,

Currently I use PostgreSQL in my apps. When I starting query on PostgreSQL is difference with mysql.
I often had error like this "ActiveRecord::StatementInvalid: PG::Error: ERROR:  column "tabulars.year" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT year FROM "tabulars"  GROUP BY id".

So I try to revised my query on below.
For instance:
1. When I want to group by:
    on MySql :
    @tabulars = Tabular.search(params[:tabular]).joins(:user).group([:year, :user_id]).paginate(per_page: DEFAULT_PER_PAGE, page: params[:page])

     on PostgreSql:
     @tabulars = Tabular.search(params[:tabular]).select([:year, :user_id]).joins(:user).group([:year, :user_id]).paginate(per_page: DEFAULT_PER_PAGE, page: params[:page])

2. On PostgreSql the select column is required on grouping table and the group by column should be any …

by on Rails can't convert ActiveSupport::HashWithIndifferentAccess into String

Ruby on Rails logo (Photo credit: Wikipedia) Hi Everyone,

I had a errors can't convert ActiveSupport::HashWithIndifferentAccess into String. This errors because I wrong on determining params json.

For example:
1. I had params:
{:tabular => {"file"=>"data_tabular_Pemerintahan (Adm Pemerintahan, Aparatur Negara dan Adm Kepegawaian)_kecamatan Admin_2013.xls"}}
2. I call on my speadsheet:
    def export
      workbook = Spreadsheet.open(params[:tabular])
    end
3. On above is not specified to parse parameter. So put this
    def export
      workbook = Spreadsheet.open(params[:tabular][:file])
    end

I hope this article can help you,

Ruby on Rails Export Excel without gem and Change the filename excel

Ruby on Rails logo (Photo credit: Wikipedia) Hi Everyone,

Currently I want to share to you how to make export function without gem and I change/modify the filename. Please follow me below:
1. Add mime type on config/initializers/mime_types.rb
    Mime::Type.register "application/xls", :xls
2. Add on your controller
    def index
      @tabulars = Tabular.all
      filename = "data_users.xls"
      respond_to do |format|
        format.html
        format.xls { headers["Content-Disposition"] = "attachment; filename=\"#{filename}\"" }
      end
    end
3. Add on your view:
    <%= link_to "Export",  tabulars_path(format: "xls") %>

4, Add format index.xls.erb file:
       <table>
          <tr>
             <th>Name</th>
          </tr>
          <% @tabulars.each do |tabular| %>
             <tr><td><%= tabular.name %></td></tr>
          <% en…

Rails Ancestry Named scope 'to_depth' is only available when depth caching is enabled

Hi Everyone,

I will share to you about my experience when had error "Named scope 'to_depth' is only available when depth caching is enabled". Please try below:

1. Add new migration
    - Migrate: add_column [tables], :ancestry_depth, :integer, :default => 0
    - Build cache: TreeNode.rebuild_depth_cache!
2. Go to model
    -  has_ancestry cache_depth: true

I hope this article can help you. :)