質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

1回答

692閲覧

検索機能搭載において、1:nの検索を行いたい

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2018/11/28 06:33

編集2018/11/29 15:41

現在Rubyで検索条件の設定を行っております。

検索条件には、

・一定期間を検索
(ex.2016/11/01~2017/09/01を検索)

・更新回数を検索
(1:nの関係において、nが登録された回数を指定した検索)

を搭載したく考えておりますが、まだrubyが未熟な部分もあり、わかりかねております。
(おそらくviewの設定のみかと思うのですが。)

どなたか教えて頂けると幸いです。

よろしくお願い致します。

post_controller.rb

1 def index 2 @q = Post.ransack(params[:q]) 3 @posts = @q.result.page(params[:page]).per(100) 4 end 5

index.html.erb

1<%= search_form_for @q do |f| %> 2 <table width = "90%"> 3 <col width="20%"> 4 <col width="30%"> 5 <col width="20%"> 6 <col width="30%"> 7 <tbody> 8 <tr> 9 <th colspan = "4">検索</th> 10 </tr> 11 <tr> 12 <th>会社名</th> 13 <td><%= f.search_field :company_cont, type: "text" %></td> 14 <th>店舗名</th> 15 <td><%= f.search_field :store_cont, type: "text" %> </td> 16 </tr> 17 <tr> 18 <th>代表者</th> 19 <td><%= f.search_field :owner_cont, type: "text" %></td> 20 <th>ダイヒョウ</th> 21 <td><%= f.search_field :kana_cont, type: "text" %></td> 22 </tr> 23 <tr> 24 <th>電話番号1</th> 25 <td><%= f.search_field :tel_cont, type: "text" %></td> 26 <th>電話番号2</th> 27 <td><%= f.search_field :tel2_cont, type: "text" %></td> 28 </tr> 29 <tr> 30 <th>FAX番号</th> 31 <td><%= f.search_field :fax_cont, type: "text" %></td> 32 <th>業種</th> 33 <td><%= f.search_field :industry_cont, type: "text" %></td> 34 </tr> 35 <tr> 36 <th>メール</th> 37 <td><%= f.search_field :mail_cont, type: "text" %></td> 38 <th>URL</th> 39 <td><%= f.search_field :url_cont, type: "text" %></td> 40 </tr> 41 <tr> 42 <th>人数</th> 43 <td><%= f.search_field :people_cont, type: "text" %></td> 44 <th>住所</th> 45 <td><%= f.search_field :address_cont, type: "text" %></td> 46 </tr> 47 <tr> 48 <th>期間</th> 49 <td><%= f.search_field :updated_at_gteq, type: "date" %></td> 50 <th>期間</th> 51 <td><%= f.search_field :updated_at_lteq, type: "date" %></td> 52 </tr> 53 <tr> 54 <th colspan = "4" ><%= f.submit '検索' %></th> 55 </tr> 56 <% end %> 57 </tbody> 58 </table> 59 60</br> 61<div class="heading"><h2>コールリスト</h2></div> 62 <table width ="90%"> 63 <tbody> 64 <col width="30%"> 65 <col width="17%"> 66 <col width="18%"> 67 <col width="25%"> 68 <col width="10%"> 69 <tr> 70 <th>会社名</th> 71 <th>代表者名</th> 72 <th>電話番号</th> 73 <th>最終更新日</th> 74 <th>編集/削除</th> 75 </tr> 76 <% @posts.each do |post| %> 77 <tr> 78 <td><%= link_to post.company, post_path(post) %> </td> 79 <td><%= link_to post.owner, post_path(post) %></td> 80 <td><%= post.tel %></td> 81 <td><%= post.details.order('updated_at DESC').first.try(:updated_at) %></td> 82 <td><%= link_to '編集', edit_post_path(post), class: 'command'%> 83 <%= link_to '削除', 84 post_path(post), 85 method: :delete, 86 class: 'command', 87 data: { confirm: '本当に削除しますか?'} %></td> 88 <% end %> 89 </tr> 90 <tr> 91 <th colspan = "5"> 92 前のページ<%= paginate @posts %>次のページ 93 </th> 94 </tr> 95 </tbody> 96 </table>

model.rb

1 2 def self.updatable_attributes 3 ["company", "store", "owner","kana", "tel", "tel2","fax", "industry", "address", "mail", "url", "people", "history"] 4 end 5

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

何と何が1:nでどういう検索がしたいのでしょうか?
また、self.searchは特に使っていないようですが何のために用意したのでしょうか?

投稿2018/11/29 12:11

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/11/29 15:32

こんにちわ。初心者のため多少わかりづらい点があるかと思いますがよろしくお願い致します。。 1(posts):n(details)となります。 <tr> <th>期間</th> <td><%= f.search_field :updated_at_gteq, type: "date" %></td> <th>期間</th> <td><%= f.search_field :updated_at_lteq, type: "date" %></td> </tr> indexでの条件で上記期間部分を検索したいです。 具体的にはdetailsの最終更新日がex.2018/11/13~2018/11/15にような形で検索を行いたく存じます。 また知識不足ですみませんが、self.searchが機能してないことは今気づきました。 よろしくお願い致します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問