Search code examples
ruby-on-railsransack

Ransacker return on related field


I am trying to create a custom ransacker that returns a product based upon an attribute within another (related) table. My DB schema is as such:

-------------   --------------------
|products   |   |product_properties|   ------------
|-----------|   |------------------|   |properties|
|name       |---|value             |---|----------|
|description|   |product_id        |   |name      |
|etc...     |   |property_id       |   ------------
-------------   --------------------

class Product < ActiveRecord::Base
  has_many :product_properties
  has_many :properties, through: :product_properties

  Property.pluck(:id, :name).each do |id, name|
    ransacker name.to_sym, formatter: -> (value) { value.to_s.downcase } do |parent|
      product_properties = Arel::Table.new(:product_properties)
      Arel::Nodes::InfixOperation.new('AND',
        Arel::Nodes::InfixOperation.new('=',
          product_properties[:property_id], id
        ),
        product_properties[:value]
      )
    end
  end
end

class ProductProperty < ActiveRecord::Base
  belongs_to :product, inverse_of: :product_properties, touch: true
  belongs_to :property, inverse_of: :product_properties
end

class Property < ActiveRecord::Base
  has_many :product_properties
  has_many :products, through: :product_properties
end

As you may be able to see I want to use ransack to select all products that have a particular property with a value that matches the predicate passed in through ransack, i.e. if I had a width property I would like to do this

Product.ransack(width_eq: 100).result

instead of

Product.ransack(product_properties_value_eq: 100, product_properties_property_name_eq: 'width')

Am I going along the right tracks, and any help with this would be much appreciated. I have been pulling my hair out over this problem.


Solution

  • The mistake made is that I needed to use Arel::Nodes.build_quoted(id). Apparently this is required when using later versions on Rails and Arel.

    Property.pluck(:id, :name).each do |id, name|
      product_properties = Arel::Table.new(:product_properties)
    
      ransacker name.to_sym, formatter: -> (value) { value.to_s.downcase } do |parent|
        Arel::Nodes::InfixOperation.new('AND',
          Arel::Nodes::InfixOperation.new('=',
            product_properties[:property_id], Arel::Nodes.build_quoted(id)
          ),
          product_properties[:value]
        )
      end
    end