私のアプリケーションのインデックスでは、すべてのプロジェクトを表示しています。各プロジェクトには複数の添付ファイルがあります。各添付ファイルにはイメージとmain_imageブール値があります。添付ファイルをループし、ブール値main_imageがtrueに設定されているものを選択し、選択した添付ファイルのイメージをプロジェクトのサムネイルにします。"メイン画像"を選択するためにスコープを使用するレール
サムネイルの正しい添付ファイルを選択するために使用できることを期待して、添付ファイルモデルのスコープを作成しましたが、これを行う方法がわかりません。
私はこれが間違った方法に近づいていると思います。誰かが私の目標をアーカイブする方法をお勧めしますか?
私はここにいくつかの記事を調査: [Loop within Loop in Rails Controller
Attachment.rb
class Attachment < ApplicationRecord
belongs_to :project
scope :main_image, lambda {where("main_image = 1")}
end
Project.rb
class Project < ApplicationRecord
has_many :attachments, dependent: :destroy
has_and_belongs_to_many :categories
before_save :set_default_position
scope :active, lambda {where(:active => true).order("position ASC")}
#array of picture attachments
def attachments_array=(array)
array.each do |file|
attachments.build(:attachment => file)
end
end
def set_default_position
if self.position == nil
self.position = 1
end
end
end
歓迎/ index.htmlを
<div id="Container" class="mixContainer">
<% @projects.each do |project| %>
<div class="mix <% project.categories.each do |cat| %>category-<%= cat.id %> <% end %>project-<%= project.id %>" data-myorder="<%= project.position %>">
<div class="mixContent">
<% project.attachments.main_image.each do |attachment| %>
<%= image_tag attachment.image.url(:thumb), class:"img-responsive" %>
<% end %>
</div>
<%= link_to(project) do %>
<div class="mixContentOver">
<div class="thumbTitle">
<h2><%= project.title %></h2>
</div>
<div class="thumbDescription">
<h4><%= project.description %></h4>
</div>
</div>
<% end %>
</div>
<% end %>
</div>
welcome_controller.rb
class WelcomeController < ApplicationController
def index
@projects = Project.active
end
end
エラー:あなたは、PostgreSQLを使用しているので
ActionView::Template::Error (PG::UndefinedFunction: ERROR: operator does not exist: boolean = integer
2016-09-11T20:13:59.328785+00:00 app[web.1]: LINE 1: ..." WHERE "attachments"."project_id" = $1 AND (main_image = 1)
2016-09-11T20:13:59.328786+00:00 app[web.1]: ^
2016-09-11T20:13:59.328786+00:00 app[web.1]: HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
2016-09-11T20:13:59.328787+00:00 app[web.1]: : SELECT "attachments".* FROM "attachments" WHERE "attachments"."project_id" = $1 AND (main_image = 1)):
2016-09-11T20:13:59.329627+00:00 app[web.1]: 49: <% @projects.each do |project| %>
2016-09-11T20:13:59.329796+00:00 app[web.1]: 50: <div class="mix <% project.categories.each do |cat| %>category-<%= cat.id %> <% end %>project-<%= project.id %>" data-myorder="<%= project.position %>">
2016-09-11T20:13:59.329956+00:00 app[web.1]: 51: <div class="mixContent">
2016-09-11T20:13:59.329993+00:00 app[web.1]: 52: <% project.attachments.main_image.each do |attachment| %>
2016-09-11T20:13:59.330124+00:00 app[web.1]: 53: <%= image_tag attachment.image.url(:thumb), class:"img-responsive" %>
2016-09-11T20:13:59.330293+00:00 app[web.1]: 54: <% end %>
2016-09-11T20:13:59.330328+00:00 app[web.1]: 55: </div>
[1]: https://stackoverflow.com/questions/10903919/loop-within-loop-in-rails-controller
だから、それは動作しませんか? –
一見するとうまくいくはずです。エラーメッセージが表示されますか? – Ilya
いいえ、それは私のアプリをクラッシュさせていません。ログ – Asan