【发布时间】:2016-09-11 20:06:03
【问题描述】:
在我的应用程序的索引中,我显示了所有项目。每个项目都有多个附件。每个附件都有一个图像和一个 main_image 布尔值。我正在尝试遍历附件并选择布尔 main_image 设置为 true 的附件,并将所选附件图像设为项目缩略图。
我在附件模型中创建了一个范围,希望可以使用它为缩略图选择正确的附件,但我不知道该怎么做。
我认为我以错误的方式处理这个问题。有人可以推荐如何归档我的目标吗?
我在这里研究了一些帖子: [Loop within Loop in Rails Controller
附件.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
错误:
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
【问题讨论】:
-
那么,它不起作用吗?
-
乍一看应该可以。您是否收到任何错误消息?
-
不,它不工作,它使我的应用程序崩溃。用日志更新问题
-
试试
scope :main_image, -> { where(main_image: true) }。让我知道它是否会起作用。 -
太棒了,它现在可以工作了。非常感谢!
标签: ruby-on-rails