In the index of my app, I'm showing all projects. Each project has multiple attachments. Each attachment has an image and a main_image boolean. I'm trying to loop thru the attachments and select the one with the boolean main_image set to true, and make the selected attachment image the project thumbnail.
I created a scope in the attachment model, hoping that I can use it to select the correct attachment for the thumbnail, but I can't figure out how to do this.
I think I'm approaching this the wrong way. Can someone please recommend how to archive my goal?
I researched some posts here: [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
welcome/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
error:
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) }. Let me know if it will work.