SlideShare una empresa de Scribd logo
1 de 20
Descargar para leer sin conexión
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

Más contenido relacionado

Similar a Search Form for Rails

Documenting from the Trenches
Documenting from the TrenchesDocumenting from the Trenches
Documenting from the Trenches
Xavier Noria
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
Yehuda Katz
 
STC 2016 Programming Language Storytime
STC 2016 Programming Language StorytimeSTC 2016 Programming Language Storytime
STC 2016 Programming Language Storytime
Sarah Kiniry
 
Presentation.Key
Presentation.KeyPresentation.Key
Presentation.Key
guesta2b31d
 
Django class based views
Django class based viewsDjango class based views
Django class based views
justinvoss
 
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptxWRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
salemsg
 

Similar a Search Form for Rails (20)

Rails3 changesets
Rails3 changesetsRails3 changesets
Rails3 changesets
 
Intro to Rails ActiveRecord
Intro to Rails ActiveRecordIntro to Rails ActiveRecord
Intro to Rails ActiveRecord
 
Elastic tire demo
Elastic tire demoElastic tire demo
Elastic tire demo
 
68837.ppt
68837.ppt68837.ppt
68837.ppt
 
mashraqi_farhan
mashraqi_farhanmashraqi_farhan
mashraqi_farhan
 
Template rendering in rails
Template rendering in rails Template rendering in rails
Template rendering in rails
 
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldEfficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
 
Documenting from the Trenches
Documenting from the TrenchesDocumenting from the Trenches
Documenting from the Trenches
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à Ruby
 
Struts 2
Struts 2Struts 2
Struts 2
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
STC 2016 Programming Language Storytime
STC 2016 Programming Language StorytimeSTC 2016 Programming Language Storytime
STC 2016 Programming Language Storytime
 
Presentation.Key
Presentation.KeyPresentation.Key
Presentation.Key
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
Django tech-talk
Django tech-talkDjango tech-talk
Django tech-talk
 
Django class based views
Django class based viewsDjango class based views
Django class based views
 
JS-05-Handlebars.ppt
JS-05-Handlebars.pptJS-05-Handlebars.ppt
JS-05-Handlebars.ppt
 
Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2
 
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptxWRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
 
Django design-patterns
Django design-patternsDjango design-patterns
Django design-patterns
 

Más de sinsoku listy

バージョン管理とGit
バージョン管理とGitバージョン管理とGit
バージョン管理とGit
sinsoku listy
 
Git天空闘技場_ハンズオン
Git天空闘技場_ハンズオンGit天空闘技場_ハンズオン
Git天空闘技場_ハンズオン
sinsoku listy
 
20101001 5分でわかるtrac pluginの作り方_slideshare
20101001 5分でわかるtrac pluginの作り方_slideshare20101001 5分でわかるtrac pluginの作り方_slideshare
20101001 5分でわかるtrac pluginの作り方_slideshare
sinsoku listy
 

Más de sinsoku listy (20)

Rails6にいつ上げるか?
Rails6にいつ上げるか?Rails6にいつ上げるか?
Rails6にいつ上げるか?
 
Rails Testing on Fargate
Rails Testing on FargateRails Testing on Fargate
Rails Testing on Fargate
 
ENGINEER WORK!!
ENGINEER WORK!!ENGINEER WORK!!
ENGINEER WORK!!
 
自己修復的なインフラ -Self-Healing Infrastructure-
自己修復的なインフラ -Self-Healing Infrastructure-自己修復的なインフラ -Self-Healing Infrastructure-
自己修復的なインフラ -Self-Healing Infrastructure-
 
毎日gemをアップグレードする生活
毎日gemをアップグレードする生活毎日gemをアップグレードする生活
毎日gemをアップグレードする生活
 
Rails 5.2: credentials
Rails 5.2: credentialsRails 5.2: credentials
Rails 5.2: credentials
 
Randomly Failing Specs
Randomly Failing SpecsRandomly Failing Specs
Randomly Failing Specs
 
技術的負債とリファクタリング
技術的負債とリファクタリング技術的負債とリファクタリング
技術的負債とリファクタリング
 
Git 初心者講座 by forkwell
Git 初心者講座 by forkwellGit 初心者講座 by forkwell
Git 初心者講座 by forkwell
 
Swift on Docker
Swift on DockerSwift on Docker
Swift on Docker
 
ES2015のカバレッジ計測
ES2015のカバレッジ計測ES2015のカバレッジ計測
ES2015のカバレッジ計測
 
CSSのカバレッジツール
CSSのカバレッジツールCSSのカバレッジツール
CSSのカバレッジツール
 
本当にあった怖い話 7つの幽霊 7つの成仏
本当にあった怖い話 7つの幽霊 7つの成仏本当にあった怖い話 7つの幽霊 7つの成仏
本当にあった怖い話 7つの幽霊 7つの成仏
 
Awsでwindowsゲームを動かす
Awsでwindowsゲームを動かすAwsでwindowsゲームを動かす
Awsでwindowsゲームを動かす
 
Action pack variantsの話
Action pack variantsの話Action pack variantsの話
Action pack variantsの話
 
LT_Gitのfast fowardと継続的デリバリー
LT_Gitのfast fowardと継続的デリバリーLT_Gitのfast fowardと継続的デリバリー
LT_Gitのfast fowardと継続的デリバリー
 
バージョン管理とGit
バージョン管理とGitバージョン管理とGit
バージョン管理とGit
 
Git天空闘技場_ハンズオン
Git天空闘技場_ハンズオンGit天空闘技場_ハンズオン
Git天空闘技場_ハンズオン
 
DVCSとGitの基礎
DVCSとGitの基礎DVCSとGitの基礎
DVCSとGitの基礎
 
20101001 5分でわかるtrac pluginの作り方_slideshare
20101001 5分でわかるtrac pluginの作り方_slideshare20101001 5分でわかるtrac pluginの作り方_slideshare
20101001 5分でわかるtrac pluginの作り方_slideshare
 

Último

valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
@Chandigarh #call #Girls 9053900678 @Call #Girls in @Punjab 9053900678
 
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
imonikaupta
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Último (20)

Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
 
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
 
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
 
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
 
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
 
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
 
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
 
Al Barsha Night Partner +0567686026 Call Girls Dubai
Al Barsha Night Partner +0567686026 Call Girls  DubaiAl Barsha Night Partner +0567686026 Call Girls  Dubai
Al Barsha Night Partner +0567686026 Call Girls Dubai
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
 
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
 
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
 
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
 
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
 
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
 

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