0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ruby eachを1行で書きたい、pluckを使いたい

Last updated at Posted at 2021-10-19

・eachを一行で書きたい

Areas.each {|area| puts area}
@shops = Shops.where(closed: false).pluck(:area_id).uniq.sort

・pluckの例

          <% areas.each do |area| %> <%# 県 %>
            <% if @shops.include?(area.id) %>
              <%= link_to "#{area.prefecture}", area_path(area.id), class: "dropdown-item" %>
            <% else %>
              <%= link_to "#{area.prefecture}", area_path(area.id), class: "dropdown-item  disabled" %>
            <% end %>
          <% end %>

【参考記事】
・定番!Rubyでワンライナー書く時の基本テク
https://qiita.com/bash0C7/items/749e60ff770b2e465b5e

0
0
2

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?