Mirec ask us:

I created some custom model attributes in model.rb file, but i found no way to include those in the list view.

Whatever i try it gives me translation error.

Is this implemented, or is there a workaround?

For this reason we want show you how you can easy extend the ExtJs GridPanel.

Case:

# models/account.rb
class Account < ActiveRecord::Base
  belongs_to :category
  ... 
  def full_name
    "#{name} #{surname}".strip
  end
end

So:

> Account.first.category.name => "my fun category"
> Account.first.full_name => "name surname"

Then:

# controllers/accounts_controller.rb
class Backend::AccountsController < BackendController

  def index
    params[:limit] ||= 50
    
    @column_store = column_store_for EbayEntry do |cm|
            # Method           # Header      #Options
      cm.add "category.name",  "Category",   :sortable => true
      cm.add "full_name",      "Full Name",  :sortable => true, 
                                             :dataIndex => [:name, :surname]
      ...
      cm.add :age
      cm.add :sex
      cm.add :other_column_of_my_model
    end

    respond_to do |format|
      format.js 
      format.json do
        render :json => @column_store.store_data(params, :joins => :category)
      end
    end
end

Considerations

If you quote something in the column_store (like: "full_name"), our extjs controller try to do some like: mymodel.send(:mycustom_method)

In column "Full Name" we have specified :dataIndex because in this way the integrated search of our grid can query the database like:

WHERE accounts.name LIKE '%name%' OR accounts.surname LIKE '%name%'

We also be able to make a custom dataIndex like: dataIndex => "accounts.mycustomfield", so the query will be:

WHERE accounts.mycustomfield LIKE '%name%'

Last important thing was: cm.add "category.name"

Our Ext controller is intelligent and automatically create a correct dataIndex in our case: categories.name

The dataIndex is also the field of a sql query, so for that is also necessary join or include the category table, we do that: @column_store.store_data(params, :joins => :category)

The final result is:

SELECT *
  FROM accounts INNER JOIN categories ON account.category_id = categories.id
WHERE 
  accounts.name LIKE '%name%' OR 
  accounts.surname LIKE '%name%' OR 
  categories.name LIKE '%name%'

So remember, with quotation ("mycustom_method") you can call any method of your model, with dataIndex you can specify the field used for ordination and search