0

Let's start with the database-model:

# => ProductSelection.rb
has_many :products, -> { uniq }, through: :product_variants

# => Product.rb
has_many :product_informations
belongs_to :product_configuration
belongs_to :product_class

Using plain Ruby on Rails, we collected the products to show inside the product_selection#show-method like so:

@products = ProductSelection.find(params[:id]).products.includes(:product_informations, :product_class, :product_configuration)

and generated a table like so:

  = table(@products).as(:products).default do |product|
    = product.name
    = product.product_configuration.name
    = product.product_class.name
    = product.state
    = link_to product_product_selection_path(@product_selection, product_id: product.id), method: :delete

Now we want to use Datatables instead of plain Ruby on Rails.

We are using the Ajax-Datatables-Rails-Gem. We would like all columns to be sortable and searchable in the end. Unfortunately we do not come past the query to retrieve the Products belonging to the ProductSelection in question.

This is the query we tried so far:

 def get_raw_records
    ProductSelection.find(params[:id]).products.includes(:product_informations, :product_class, :product_configuration).references(:product_information, :product_class, :product_configuration).distinct
 end


 def data
    records.map do |record|
      [
        record.name,
        record.product_configuration.name,
        record.product_class.name,
        record.state,
        record.id
      ]
    end
  end

The error that pops up:

> undefined method `each_with_index' for nil:NilClass

When adding in sortable/searchable columns, the error is the following instead:

PG::UndefinedColumn at /product_selections/fetch_table_data_show.json
=====================================================================

> ERROR:  column products.name does not exist
LINE 1: SELECT  DISTINCT "products"."id", products.name

with the configuration like so:

  def sortable_columns
    # Declare strings in this format: ModelName.column_name
    @sortable_columns ||= %w(Product.name ProductConfiguration.name ProductClass.name Product.state)
  end

  def searchable_columns
    # Declare strings in this format: ModelName.column_name
    @searchable_columns ||= %w(Product.name ProductConfiguration.name ProductClass.name Product.state)
  end

I think the problem is with the different models right here. I assume Datatables-Rails expects a model of ProductSelection but instead is prompted with a Product. Any help would be highly appreciated! If anything is missing, let me know!

Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59

1 Answers1

0

After having another look at this, we figured the problem was that Product.name wasn't a valid ActiveRecord-construct. Instead it was a helper method defined somewhere down the road.

Since the gem tries to find records through the column-names, this error occurred.

We solved it by removing the column in question. Other approaches would be to:

  1. Store the data in a separate column, instead of using a helper.
  2. Implementing the helpers behavior in javascript so that we can pass it as a callback to datatables.
Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59