1
0

More than 5 years have passed since last update.

integer型のデータを検索する

Last updated at Posted at 2019-04-25

今までstring型のデータの検索は容易にできていたんですが、ふと仕事が渡ってきた時にそういえばやったことなかったなと、、、、
:pref(都道府県)がstring型の場合こんなことはしなくていいんだけど、はinteger型データが渡される!という話だったので、色々頑張りました。結構rubyの知識が必要

index.html.erb

(最初のコード)

<th scope="row">都道府県</th>
<td><%= select_tag :pref, options_for_select(PREF_ARRAY.map.with_index {|pref_name, index| [pref_name, index]}, params[:pref]), :include_blank => '選択してください' %></td>
</tr>

ターミナル.
[1] pry(#<#<Class:0x00007fa68b760ac8>>)>select_tag :pref, options_for_select(PREF_ARRAY.map.with_index {|pref_name, index| [pref_name, index]}, params[:pref])

=><option selected=\"selected\" value=\"北海道\">北海道</option>\n<option value=\"青森県\">青森県</option>\n<option value=\"岩手県\">岩手県</option>\n<option value=\"宮城県\">宮城県</option>\n<option value=\"秋田県\">秋田県</option>\n<option value=\"山形県\">山形県</option>\n<option value=\"福島県\">福島県</option>\n<option value=\"茨城県\">茨城県</option>\n<option value=\"栃木県\">栃木県</option>...
index.html.erb

(修正後のコードindexが表示されるようになった!)

<th scope="row">都道府県</th>
<td><%= select_tag :pref, options_for_select(PREF_ARRAY.map.with_index {|pref_name, index| [pref_name, index]}, params[:pref]), :include_blank => '選択してください' %></td>
</tr>
ターミナル.
[1] pry(#<#<Class:0x00007fa68b760ac8>>)> select_tag :pref, options_for_select(PREF_ARRAY.map.with_index {|pref_name, index| [pref_name, index]}, params[:pref])

=><select name=\"pref\" id=\"pref\"><option value=\"0\">北海道</option>\n<option value=\"1\">青森県</option>\n<option value=\"2\">岩手県</option>\n<option value=\"3\">宮城県</option>\n<option value=\"4\">秋田県</option>\n<option value=\"5\">山形県</option>\n<option value=\"6\">福島県</option>\n<option value=\"7\">茨城県</option>\n<option value=\"8\">栃木....

参考記事:(option_forSelect)http://song-of-life.hatenablog.com/entry/2017/11/12/004027
参考記事:(mapでeach_with_indexみたいなことしたい)https://qiita.com/vsanna/items/6dd7247ad8f7dd81fe03

environment.rb
PREF_ARRAY = [
  '北海道','青森県','岩手県','宮城県','秋田県','山形県','福島県','茨城県','栃木県','群馬県',
  '埼玉県','千葉県','東京都','神奈川県','新潟県','富山県','石川県','福井県','山梨県','長野県',
  '岐阜県','静岡県','愛知県','三重県','滋賀県','京都府','大阪府','兵庫県','奈良県','和歌山県',
  '鳥取県','島根県','岡山県','広島県','山口県','徳島県','香川県','愛媛県','高知県','福岡県',
  '佐賀県','長崎県','熊本県','大分県','宮崎県','鹿児島県','沖縄県'
]
customers_controller.rb
def index
 @customers = Customer.search(params)
 customers = []
 @customers.each do |customer|
 customers << customer
end
customer.rb

def self.search(params)
    customers = customers.where("pref LIKE ?", "%#{params[:pref]}%") if params[:pref].present?
end

上のparams[:pref] にはindexが入ってた!

ターミナル.

[1] pry(Customer)> params[:pref] 
=> "46"

これでDBに登録されている数字の都道府県を取得できた!

って思ったらindexは0からじゃなくて1からという報告。

indexは0からじゃなくて1からに

こっちでも

<tr>
<th scope="row">都道府県</th>
<% binding.pry%>
<td><%= select_tag :pref, options_for_select(PREF_ARRAY.map.with_index {|pref_name, index| [pref_name, index + 1]} << ["海外", 99], params[:pref]), :include_blank => '選択してください' %></td>
</tr>

こっちでも同じ意味

<tr>
<th scope="row">都道府県</th>
<% binding.pry%>
<td><%= select_tag :pref, options_for_select(PREF_ARRAY.map.with_index(1) {|pref_name, index| [pref_name, index]} , params[:pref]), :include_blank => '選択してください' %></td>
</tr>

参考記事:(index1からにしたいよお)https://qiita.com/QUANON/items/8e257835b11f65ede2f4

PREF_ARRAYにはなかった海外を入れる

<tr>
<th scope="row">都道府県</th>
<% binding.pry%>
<td><%= select_tag :pref, options_for_select(PREF_ARRAY.map.with_index(1) {|pref_name, index| [pref_name, index]} << ["海外", 99], params[:pref]), :include_blank => '選択してください' %></td>
</tr>

合致検索にする

北海道と検索したらindexが 1のデータを取得するはずがlikeを使っていて曖昧検索になっているので 1321といったデータも取得されていた

customer.rb
修正前
def self.search(params)
  customers = customers.where("pref LIKE ?", "%#{params[:pref]}%") if params[:pref].present?
end
customer.rb
修正後
def self.search(params)
  customers = customers.where(pref: params[:pref]) if params[:pref].present?
end

これで北海道のデータしか見つけてこれなくなりました

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0