Convert Figma logo to code with AI

activerecord-hackery logoransack

Object-based searching.

5,648
791
5,648
152

Top Related Projects

2,400

Active Record, improved. Live again :)

pg_search builds ActiveRecord named scopes that take advantage of PostgreSQL’s full text search

Intelligent search made easy

Elasticsearch integrations for ActiveModel/Record and Ruby on Rails

Sphinx/Manticore plugin for ActiveRecord/Rails

Quick Overview

Ransack is a Ruby gem that provides advanced search functionality for Ruby on Rails applications. It allows developers to easily create complex search forms and queries, supporting both simple and advanced search scenarios across multiple models and associations.

Pros

  • Flexible and powerful search capabilities
  • Seamless integration with Ruby on Rails applications
  • Supports complex queries and associations
  • Extensive customization options

Cons

  • Learning curve for advanced features
  • Can impact performance with complex queries on large datasets
  • Documentation can be overwhelming for beginners
  • Some limitations when working with non-ActiveRecord models

Code Examples

  1. Basic search form:
<%= search_form_for @q do |f| %>
  <%= f.label :title_cont %>
  <%= f.search_field :title_cont %>
  <%= f.submit %>
<% end %>

This code creates a simple search form for filtering by title.

  1. Advanced search with associations:
<%= search_form_for @q do |f| %>
  <%= f.label :name_cont %>
  <%= f.search_field :name_cont %>
  <%= f.label :author_name_cont %>
  <%= f.search_field :author_name_cont %>
  <%= f.submit %>
<% end %>

This example demonstrates searching across associations, allowing users to search for books by both title and author name.

  1. Custom predicate:
Ransack.configure do |config|
  config.add_predicate 'starts_with',
    arel_predicate: 'matches',
    formatter: proc { |v| "#{v}%" },
    validator: proc { |v| v.present? },
    type: :string
end

This code adds a custom predicate 'starts_with' to Ransack, enabling more specific search conditions.

Getting Started

  1. Add Ransack to your Gemfile:
gem 'ransack'
  1. Run bundle install

  2. In your controller:

def index
  @q = Model.ransack(params[:q])
  @results = @q.result(distinct: true)
end
  1. In your view:
<%= search_form_for @q do |f| %>
  <%= f.label :name_cont %>
  <%= f.search_field :name_cont %>
  <%= f.submit %>
<% end %>

<% @results.each do |result| %>
  <%= result.name %>
<% end %>

This setup creates a basic search form and displays the results.

Competitor Comparisons

2,400

Active Record, improved. Live again :)

Pros of Squeel

  • More expressive and flexible query syntax
  • Supports complex nested conditions and subqueries
  • Better integration with Ruby's native syntax

Cons of Squeel

  • Less actively maintained compared to Ransack
  • Steeper learning curve for developers new to the DSL
  • May have compatibility issues with newer Rails versions

Code Comparison

Ransack:

User.ransack(first_name_cont: 'John', last_name_start: 'D').result

Squeel:

User.where{(first_name =~ '%John%') & (last_name =~ 'D%')}

Squeel offers a more Ruby-like syntax for complex queries, allowing for nested conditions and use of Ruby operators. Ransack, on the other hand, uses a simpler key-value approach that may be easier for beginners to understand but can become verbose for complex queries.

Both libraries extend ActiveRecord to provide advanced querying capabilities, but they differ in their approach and syntax. Ransack is generally considered more beginner-friendly and actively maintained, while Squeel offers more power and flexibility at the cost of a steeper learning curve and potential compatibility issues with newer Rails versions.

pg_search builds ActiveRecord named scopes that take advantage of PostgreSQL’s full text search

Pros of pg_search

  • Specialized for PostgreSQL, leveraging its full-text search capabilities
  • Supports multiple search configurations and weighting options
  • Offers advanced features like trigram similarity and TSVector columns

Cons of pg_search

  • Limited to PostgreSQL databases only
  • Less flexible for complex, dynamic search queries
  • Smaller community and fewer integrations compared to Ransack

Code Comparison

pg_search:

class Product < ApplicationRecord
  include PgSearch::Model
  pg_search_scope :search_by_name, against: :name
end

Product.search_by_name("example")

Ransack:

class ProductsController < ApplicationController
  def index
    @q = Product.ransack(params[:q])
    @products = @q.result(distinct: true)
  end
end

<%= search_form_for @q do |f| %>
  <%= f.search_field :name_cont %>
  <%= f.submit %>
<% end %>

pg_search is more focused on PostgreSQL-specific search features, while Ransack provides a more general-purpose search solution for various databases. pg_search excels in utilizing PostgreSQL's full-text search capabilities, whereas Ransack offers greater flexibility for complex queries across different database systems.

Intelligent search made easy

Pros of Searchkick

  • Offers powerful full-text search capabilities using Elasticsearch
  • Provides intelligent typo tolerance and autocomplete suggestions
  • Supports geospatial search and faceted search out of the box

Cons of Searchkick

  • Requires setting up and maintaining an Elasticsearch cluster
  • May have a steeper learning curve for developers new to Elasticsearch
  • Can be overkill for simple search requirements

Code Comparison

Ransack:

@q = Person.ransack(params[:q])
@people = @q.result(distinct: true)

Searchkick:

@people = Person.search(params[:q], fields: [:name, :email], match: :word_start)

Summary

Ransack is a Ruby gem that provides object-based searching for Active Record models. It's lightweight and integrates seamlessly with Rails applications. Searchkick, on the other hand, is built on top of Elasticsearch and offers more advanced search features like fuzzy matching and geospatial search.

Ransack is easier to set up and use for simple search requirements, while Searchkick provides more powerful search capabilities but requires additional infrastructure. The choice between the two depends on the specific needs of your project, such as the complexity of search requirements and the scale of your data.

Elasticsearch integrations for ActiveModel/Record and Ruby on Rails

Pros of elasticsearch-rails

  • Powerful full-text search capabilities with advanced features like fuzzy matching and relevance scoring
  • Scalable and distributed architecture for handling large datasets and high query volumes
  • Real-time indexing and search updates for near-instantaneous data availability

Cons of elasticsearch-rails

  • Steeper learning curve and more complex setup compared to Ransack
  • Requires additional infrastructure and maintenance for Elasticsearch cluster
  • May be overkill for simple search requirements in smaller applications

Code Comparison

Ransack query example:

@q = Person.ransack(first_name_cont: 'John', last_name_start: 'Doe')
@people = @q.result(distinct: true)

elasticsearch-rails query example:

@people = Person.search(query: {
  multi_match: {
    query: 'John Doe',
    fields: ['first_name', 'last_name']
  }
}).records

Both libraries provide search functionality, but elasticsearch-rails offers more advanced querying capabilities at the cost of increased complexity. Ransack integrates seamlessly with ActiveRecord and is easier to set up for basic search needs, while elasticsearch-rails excels in scenarios requiring sophisticated full-text search and scalability.

Sphinx/Manticore plugin for ActiveRecord/Rails

Pros of Thinking Sphinx

  • Specialized for full-text search with Sphinx, offering advanced search capabilities
  • Supports geospatial search and faceted search out of the box
  • Provides real-time indexing for immediate search results

Cons of Thinking Sphinx

  • Limited to Sphinx search engine, less flexible than Ransack
  • Requires additional setup and configuration of Sphinx server
  • Steeper learning curve for developers unfamiliar with Sphinx

Code Comparison

Thinking Sphinx:

ThinkingSphinx::Index.define :article, :with => :active_record do
  indexes title, sortable: true
  indexes content
  has author_id, created_at, updated_at
end

Ransack:

@q = Article.ransack(params[:q])
@articles = @q.result(distinct: true)

Thinking Sphinx focuses on defining indexes for Sphinx search, while Ransack provides a more flexible querying interface directly on ActiveRecord models. Thinking Sphinx excels in complex full-text search scenarios, whereas Ransack is more suitable for general-purpose filtering and sorting of ActiveRecord collections.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Ransack

Build Status Gem Version Code Climate Backers on Open Collective Sponsors on Open Collective

Introduction

Ransack will help you easily add searching to your Rails application, without any additional dependencies.

There are advanced searching solutions around, like ElasticSearch or Algolia. Ransack will do the job for many Rails websites, without the need to run additional infrastructure or work in a different language. With Ransack you do it all with standard Ruby and ERB.

Ready to move beyond the basics? Use advanced features like i18n and extensive configuration options.

Ransack is supported for Rails 7.2, 7.1, 7.0, 6.1 on Ruby 3.1 and later.

Installation

To install ransack and add it to your Gemfile, run

gem 'ransack'

Bleeding edge

If you would like to use the latest updates not yet published to RubyGems, use the main branch:

gem 'ransack', :github => 'activerecord-hackery/ransack', :branch => 'main'

Documentation

There is extensive documentation on Ransack, which is a Docusaurus project and run as a GitHub Pages site.

Issues tracker

  • Before filing an issue, please read the Contributing Guide.
  • File an issue if a bug is caused by Ransack, is new (has not already been reported), and can be reproduced from the information you provide.
  • Please consider adding a branch with a failing spec describing the problem.
  • Contributions are welcome. :smiley:
  • Please do not use the issue tracker for personal support requests. Stack Overflow or GitHub Discussions is a better place for that where a wider community can help you!

Contributions

To support the project:

  • Consider supporting us via Open Collective
  • Use Ransack in your apps, and let us know if you encounter anything that's broken or missing. A failing spec to demonstrate the issue is awesome. A pull request with passing tests is even better!
  • Before filing an issue or pull request, be sure to read and follow the Contributing Guide.
  • Please use Stack Overflow or GitHub Discussions for questions or discussion not directly related to bug reports, pull requests, or documentation improvements.
  • Spread the word on social media if Ransack's been useful to you. The more people who are using the project, the quicker we can find and fix bugs!

Contributors

Ransack was created by Ernie Miller and is developed and maintained by:

Alumni Maintainers

This project exists thanks to all the people who contribute.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]