Displaying multiple paperclip images from database in ROR app. many relations

Viewed 496

I'm very new to ROR and I'm building an ROR app where each product can have many images. Im using paperclipfor the image upload.

To do that I added an image.rb model to the app and now the product.rbmodel has_many :images and accepts_nested_attributes_for :images.

That is all working fine with out problems.

The thing is that the product.rb model have this relations belongs_to :category and belongs_to :label

and category and label both has_many :products

Before I added the image.rb model each product had one picture attached to it. and at the index.html.erbpage the user could see categories lined up, with the latest uploaded picture from each product in category as the front picture for each category.

Below is the snipped I used to show the categories at index.html.erb before I added the image.rbmodel.

<div class="container-fluid">

        <% @products.each_slice(3) do |products_group| %>
        <div class="row">
          <% products_group.each do |category, products| %>

                <% products.each_with_index do |product, index| %>
                    <% if index == 0 %>
                        <div class="col-lg-4 col-sm-6 col-xs-12 center-block " >

                        <%= link_to category_path (category), { :method => 'GET' } do %>
                            <%= image_tag product.image.url(:medium), class: "img-responsive" %>
                        <% end %>
                <div class="caption">
                    <p class="category-name" ><%= product.category.name %></p>
                 </div> 
                <% end %>
                <% end %>
                </div> 
            <% end %>
            </div>
        <% end %>

</div>

And in the pages_controller.rb there was this code:

 def index   
   @products = Product.all.order(created_at: :desc).group_by(&:category_id)
 end

As seen in the code samples above I´m grouping products based on their categories, and then I'm showing each category that have any products at the index page. The category shown on the index page shows only the latest uploaded product in each category.

But now after I have the image.rbmodel to handle the photo upload I need a way to show the same result at the index page but when the user hits some category he is taken to that category page.

It is a problem because now the images are bound to the image.rb model but not the product.rb model anymore.

I can see the uploaded pictures in the image.rbmodel but I'm not sure how I should adjust the relations with out breaking everything.

Can someone please help me?

below are the models involved:

product.rb

class Product < ActiveRecord::Base
  acts_as_list :scope => [:category, :label]
  belongs_to :category
  belongs_to :label

  has_many :images
  accepts_nested_attributes_for :images
  has_many :product_items, :dependent => :destroy
end

the category.rb

class Category < ActiveRecord::Base

  has_many :products, :dependent => :nullify

   extend FriendlyId
   friendly_id :name, use: [:slugged, :finders]

end

and the image.rb

class Image < ActiveRecord::Base

  belongs_to :product

    has_attached_file :image, styles: { medium: "500x500#", thumb: "100x100#" }
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/

end
1 Answers
Related