1

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
5
  • So, doesn't it work? Commented Sep 11, 2016 at 20:08
  • It should work on first glance. Do you receive any error message? Commented Sep 11, 2016 at 20:09
  • No, it's not working it's crashing my app. Updating question with log Commented Sep 11, 2016 at 20:15
  • try scope :main_image, -> { where(main_image: true) }. Let me know if it will work. Commented Sep 11, 2016 at 20:16
  • Awesome, it's working now. Thanks a Lot! Commented Sep 11, 2016 at 20:22

1 Answer 1

1

Since you use Postgresql, you should specify boolean type implicitly:

scope :main_image, -> { where(main_image: true) }
Sign up to request clarification or add additional context in comments.

1 Comment

Oh ok, I understand now. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.