SlideShare a Scribd company logo
Search Form for Rails
Roppongi.rb #11 2019/06/20(木)
1
自己紹介
名前: 神速
GitHub: @sinsoku (アイコン右上)
Twitter: @sinsoku_listy (アイコン右下)
Rails歴: それなり
2
Searching
1. Elasticsearch
2. Amazon CloudSearch
3. Ransack
4. self implementation
5. other...?
3
4
controller
def index
@q = Person.ransack(params[:q])
@people = @q.result(distinct: true)
end
5
view
<%= search_form_for @q do |f| %>
# Search if the name field contains...
<%= f.label :name_cont %>
<%= f.search_field :name_cont %>
# Search if an associated articles.title starts with...
<%= f.label :articles_title_start %>
<%= f.search_field :articles_title_start %>
# Attributes may be chained. Search multiple attributes for one value...
<%= f.label :name_or_description_or_email_or_articles_title_cont %>
<%= f.search_field :name_or_description_or_email_or_articles_title_cont %>
<%= f.submit %>
<% end %>
6
Pros
• easy to introduce
• realizes complex conditions with Advanced Mode
• searches by associations
• used by many people(, and easy to find references)
7
Cons
• includes many hack codes for ActiveRecord
• difficult to maintenance
• CI failed on 5-2-stable
!
• I do not like the design about Authorization
8
Example
Support for Rails 6
https://github.com/activerecord-hackery/ransack/pull/1027
9
I came up with a new implementation
So, I am developing a gem to replace Ransack.
10
sinsoku/pickel
Pickel provides methods that make it
easy to build a search form for your
Rails app.
11
in controller
def index
search_params = Pickel.permit(params, :name, :age_gt, :posts_title_start)
@search = Pickel.search(User, search_params)
@users = @search.result
end
12
in view
<%= form_for @search do |f| %>
<%# Search records contains the value %>
<%= f.label :name_cont %>
<%= f.search_field :name_cont %>
<%# Search records grater than the value %>
<%= f.label :age_gt %>
<%= f.number_field :age_gt %>
<%# Search records an associated posts.title starts with the value %>
<%= f.label :posts_title_start %>
<%= f.search_field :posts_title_start %>
<%= f.submit %>
<% end %>
13
Concept (Difference from Ransack)
• refrains hack for ActiveRecord
• basically builds a SQL in ActiveRecord.merge
• (However, Arel is used)
• allows search conditions by the StrongParameter
• uses form_for as it is
14
Design 1
• handle search conditions with Hash
conditinos = {
name_eq: 'foo',
posts_title_start: 'bar'
}
15
Design 2
• Convert a condition to Relation
rel_1 = User.where(name: 'foo')
cond = Post.arel_table[:title].start('bar%')
rel_2 = User.joins(:posts).merge(Post.where(cond))
16
Design 3
• Merge all conditions
rel = rel_1.merge(rel_2)
17
Is this usable in production?
18
Sorry, it is still in development.
Once it has been implemented to replace
Ransack, I will be tagged v1.0.0.
19
Click Watch
!
or Star
20

More Related Content

PPTX
Rozalia alik math3 (latest)
PPTX
Rozalia alik math3 (newone)
KEY
Lu solr32 34-20110912
PPTX
V15 like operator-c
PDF
Rails6にいつ上げるか?
PDF
Rails Testing on Fargate
PDF
ENGINEER WORK!!
PDF
自己修復的なインフラ -Self-Healing Infrastructure-
Rozalia alik math3 (latest)
Rozalia alik math3 (newone)
Lu solr32 34-20110912
V15 like operator-c
Rails6にいつ上げるか?
Rails Testing on Fargate
ENGINEER WORK!!
自己修復的なインフラ -Self-Healing Infrastructure-

More from sinsoku listy (16)

PDF
毎日gemをアップグレードする生活
PDF
Rails 5.2: credentials
PDF
Randomly Failing Specs
PDF
技術的負債とリファクタリング
PDF
Git 初心者講座 by forkwell
PDF
Swift on Docker
PDF
ES2015のカバレッジ計測
PDF
CSSのカバレッジツール
PDF
本当にあった怖い話 7つの幽霊 7つの成仏
PDF
Awsでwindowsゲームを動かす
PDF
Action pack variantsの話
PDF
LT_Gitのfast fowardと継続的デリバリー
PDF
バージョン管理とGit
PDF
Git天空闘技場_ハンズオン
PDF
DVCSとGitの基礎
ODP
20101001 5分でわかるtrac pluginの作り方_slideshare
毎日gemをアップグレードする生活
Rails 5.2: credentials
Randomly Failing Specs
技術的負債とリファクタリング
Git 初心者講座 by forkwell
Swift on Docker
ES2015のカバレッジ計測
CSSのカバレッジツール
本当にあった怖い話 7つの幽霊 7つの成仏
Awsでwindowsゲームを動かす
Action pack variantsの話
LT_Gitのfast fowardと継続的デリバリー
バージョン管理とGit
Git天空闘技場_ハンズオン
DVCSとGitの基礎
20101001 5分でわかるtrac pluginの作り方_slideshare
Ad

Recently uploaded (20)

PPT
Ethics in Information System - Management Information System
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPTX
Funds Management Learning Material for Beg
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
DOC
Rose毕业证学历认证,利物浦约翰摩尔斯大学毕业证国外本科毕业证
PDF
Paper PDF World Game (s) Great Redesign.pdf
PPTX
artificial intelligence overview of it and more
PDF
Exploring VPS Hosting Trends for SMBs in 2025
PPTX
SAP Ariba Sourcing PPT for learning material
PPT
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
PPTX
Database Information System - Management Information System
PPT
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PPTX
newyork.pptxirantrafgshenepalchinachinane
PPTX
Introduction to cybersecurity and digital nettiquette
PPTX
innovation process that make everything different.pptx
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
Ethics in Information System - Management Information System
The New Creative Director: How AI Tools for Social Media Content Creation Are...
Module 1 - Cyber Law and Ethics 101.pptx
Funds Management Learning Material for Beg
presentation_pfe-universite-molay-seltan.pptx
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Unit-1 introduction to cyber security discuss about how to secure a system
Rose毕业证学历认证,利物浦约翰摩尔斯大学毕业证国外本科毕业证
Paper PDF World Game (s) Great Redesign.pdf
artificial intelligence overview of it and more
Exploring VPS Hosting Trends for SMBs in 2025
SAP Ariba Sourcing PPT for learning material
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
Database Information System - Management Information System
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
newyork.pptxirantrafgshenepalchinachinane
Introduction to cybersecurity and digital nettiquette
innovation process that make everything different.pptx
An introduction to the IFRS (ISSB) Stndards.pdf
Ad

Search Form for Rails

  • 1. Search Form for Rails Roppongi.rb #11 2019/06/20(木) 1
  • 2. 自己紹介 名前: 神速 GitHub: @sinsoku (アイコン右上) Twitter: @sinsoku_listy (アイコン右下) Rails歴: それなり 2
  • 3. Searching 1. Elasticsearch 2. Amazon CloudSearch 3. Ransack 4. self implementation 5. other...? 3
  • 4. 4
  • 5. controller def index @q = Person.ransack(params[:q]) @people = @q.result(distinct: true) end 5
  • 6. view <%= search_form_for @q do |f| %> # Search if the name field contains... <%= f.label :name_cont %> <%= f.search_field :name_cont %> # Search if an associated articles.title starts with... <%= f.label :articles_title_start %> <%= f.search_field :articles_title_start %> # Attributes may be chained. Search multiple attributes for one value... <%= f.label :name_or_description_or_email_or_articles_title_cont %> <%= f.search_field :name_or_description_or_email_or_articles_title_cont %> <%= f.submit %> <% end %> 6
  • 7. Pros • easy to introduce • realizes complex conditions with Advanced Mode • searches by associations • used by many people(, and easy to find references) 7
  • 8. Cons • includes many hack codes for ActiveRecord • difficult to maintenance • CI failed on 5-2-stable ! • I do not like the design about Authorization 8
  • 9. Example Support for Rails 6 https://github.com/activerecord-hackery/ransack/pull/1027 9
  • 10. I came up with a new implementation So, I am developing a gem to replace Ransack. 10
  • 11. sinsoku/pickel Pickel provides methods that make it easy to build a search form for your Rails app. 11
  • 12. in controller def index search_params = Pickel.permit(params, :name, :age_gt, :posts_title_start) @search = Pickel.search(User, search_params) @users = @search.result end 12
  • 13. in view <%= form_for @search do |f| %> <%# Search records contains the value %> <%= f.label :name_cont %> <%= f.search_field :name_cont %> <%# Search records grater than the value %> <%= f.label :age_gt %> <%= f.number_field :age_gt %> <%# Search records an associated posts.title starts with the value %> <%= f.label :posts_title_start %> <%= f.search_field :posts_title_start %> <%= f.submit %> <% end %> 13
  • 14. Concept (Difference from Ransack) • refrains hack for ActiveRecord • basically builds a SQL in ActiveRecord.merge • (However, Arel is used) • allows search conditions by the StrongParameter • uses form_for as it is 14
  • 15. Design 1 • handle search conditions with Hash conditinos = { name_eq: 'foo', posts_title_start: 'bar' } 15
  • 16. Design 2 • Convert a condition to Relation rel_1 = User.where(name: 'foo') cond = Post.arel_table[:title].start('bar%') rel_2 = User.joins(:posts).merge(Post.where(cond)) 16
  • 17. Design 3 • Merge all conditions rel = rel_1.merge(rel_2) 17
  • 18. Is this usable in production? 18
  • 19. Sorry, it is still in development. Once it has been implemented to replace Ransack, I will be tagged v1.0.0. 19