简体   繁体   中英

How to iterate over ActiveRecord model objects with a particular value of an attribute?

I am going to try to be very explicit because I've been trying to do a "special" iteration for several days.

I created cards which are named hiragana and are defined by a Letter (A, E, I, O, U) and decline in (KA, SA, TA... KE, SE, TE... (I, O, U => similar)).

I do an iteration with .each with hiragana instance to list them on the index page:

<% @hiraganas.each do |hiragana| %>
  <li>
    <%= render 'allhiraganas', hiragana: hiragana %>
  </li>
<% end %>

The view is

<div class="row">
  <ul class="list-inline text-center card-frame">
    <li>
      <div class="card">
        <div class="front">
          <% if current_user.try(:admin?) %>
            <%= link_to hiragana_path(hiragana), class:'trash-hiragana', data: { confirm: 'Are you sure?' }, method: :delete do %>
            <%= image_tag("delete-btn.png") %>
            <% end %>
          <% end %>

          <span class="card-question popover-word" data-content="<h4 class='text-center letter-uppercase'>**<%= hiragana.bigletter.upcase %>**</h4><p class='text-center'><b><%= hiragana.midletter %></b> comme dans <b><%= hiragana.transcription %></b></p>">
            <i class="fa fa-eye fa-lg"></i>
          </span>

          <!-- son de l'hiragana -->
          <span class="audioclick popover-word" data-content="<p class='text-center'><b>le son arrive prochainement !</b></p>">
            <i class="fa fa-volume-up fa-lg"></i>
          </span>
          <!-- image mnémotechnique -->
           <% if hiragana.upload.blank? %>
              <div class="idea">
                <i class="fa fa-lightbulb-o fa-lg"></i>
              </div>
              <% else %>
                <div class="idea">
                  <span class="popover-trigger"
                  data-toggle="popover"
                  data-content='<%= cl_image_tag(hiragana.upload,
                  :width => 210, :height => 290) %>'>
                  <i class="fa fa-lightbulb-o fa-lg"></i>
                </span>
              </div>
            <% end %>

          <!-- <div class="prononciation">
            <i class="fa fa-comment"></i>
          </div> -->

          <div class="card-hiragana hiragana-<%=hiragana.bigletter.downcase.last%>">
            <h1><b><%= hiragana.ideo1 %></b></h1>
          </div>

          <div class="card-katakana">
            <p><%= hiragana.ideo2 %></p>
          </div>
          <div id="favorite_<%=hiragana.id%>">
            <%= render 'favs/favorites', hiragana: hiragana %>
          </div>
        </div>
        <div class="back">
          <div class="col-sm-3 col-xs-4 col-md-3 containerbackcards-<%=hiragana.bigletter.downcase.last%>">
            <div class="backcard-hiragana">
              <h1><b><%= hiragana.ideo1 %></b></h1>
            </div>
            <div class="card-bigletter">
              <h4><%= hiragana.bigletter.upcase %></h4>
            </div>
          </div>
        </div>
      </div>
    </li>
  </ul>
</div>

I want to iterate on the hiragana.bigletter method.

<span class="card-question popover-word" data-content="<h4 class='text-center letter-uppercase'>**<%= hiragana.bigletter.upcase %>**</h4><p class='text-center'><b><%= hiragana.midletter %></b> comme dans <b><%= hiragana.transcription %></b></p>">
  <i class="fa fa-eye fa-lg"></i>
</span>

because I want to have all hiraganas with the bigletter A display on a vertical column, all hiraganas with the bigletter E display on another vertical column and so on.

Some people told me about .select but I don't know where to put it. In the hiragana model?`

Please could you guide me what can I do to dry my code in model. Here is the hiragana model:

class Hiragana < ActiveRecord::Base 
  belongs_to :user
  has_many :favs, dependent: :destroy
  mount_uploader :upload, ImageUploader
  mount_uploader :sound, SoundUploader

  def is_faved_by(user) 
    self.favs.where(user: user).take 
  end
end

Thank you for your help.

I think this is what you want:

<% @hiraganas.select { |hiragana| hiragana.bigletter == 'a' }.each do |hiragana| %>
  <%= render 'somehiraganas', hiragana: hiragana %>
<% end %>

It's preferable to minimize code in templates, so it would be better to add a method to the model

class Hiragana < ActiveRecord::Base
  def self.with_bigletter(bigletter)
    where(bigletter: bigletter).order(:midletter)
  end
end

and assign the result of Hiragana.with_bigletter('a') and so on to a template variable or variables in your controller.

It might be better for performance to query all Hiraganas with ActiveRecord and have with_bigletter do select as in my first answer, but I wouldn't worry about that unless you actually had a performance problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM