leikind / wice_grid

A Rails grid plugin to create grids with sorting, pagination, and (automatically generated) filters
MIT License
537 stars 213 forks source link

How to concatenate separate db columns into one column in wice_grid? #155

Closed asgeo1 closed 10 years ago

asgeo1 commented 10 years ago

In my 'student' model, I have separate first_name and last_name attributes. I have a method name on the model class which returns the full name

def name
    "#{first_name} #{last_name}"
end

I want to use this name method in a wice_grid filter. Can I do that? I can display the name method in a column easy enough by just passing it in a block. But I want to filter on this method as well.

The problem is wice_grid looks at the underlying database structure, rather than the ActiveRecord model.

I thought of this:

  g.column name: 'Student', attribute: 'first_name || last_name', model: 'ContactInformation' do |fee|
    fee.student.name
  end

I.e. || is the concatenation operator in PostgreSQL.

That unfortunately doesn't work because wice_grid complains that WiceGrid: Column 'first_name||last_name' is not found in table 'contact_informations'!

So I'm not sure what else to do. I don't want to have separate first_name and last_name columns in my grid - if I can filter them together into one column that would be awesome.

Any ideas?

leikind commented 10 years ago

Your only solution in to have a third column which is a concatenation of first_name and last_name. Just add a before_save where you concatenate first_name and last_name and assign the result to this field.

asgeo1 commented 10 years ago

OK thanks. I've done that now - not ideal, but not a big issue to implement.