在视图或index.html.erb中一起显示嵌套属性

时间:2015-06-02 21:31:25

标签: ruby-on-rails ruby-on-rails-4 activerecord rails-activerecord nested-attributes

我有产品模型,其中包含大部分属性以及与我的 Product_Images 模型的has_many关系。所有更新,编辑等都像魅力一样,但我很难将数据带入前端引导主题。

我尝试做的是拥有最后5个产品,并且默认图片会显示,因此我可以拥有 产品图片 和相关产品< strong> 属性 在一起。目前,我拥有2个独立数组中的所有属性和所有图像。

我查看了group_by但是我无法解决如何通过product_id将属性+图像从两个数组组合在一起。我根本不知道该怎么做。

应用/控制器/ home_controller.html.erb

class HomeController < ApplicationController
  def index
    @products = Product.last(5)
    @product_ids = @products.map(&:id)
    @product_images = ProductImage.where(:product_id => @product_ids, 
    :default_image => true )
  end
end

下面基本上转储了两个数组,但它们不在一起。每个product_id的图像/属性

应用/视图/家/ index.html.erb

 <% @products.each do |pd| %>
      <%= pd.product_name %>
  <% end %>
  <% @product_images.each do |pi| %>
      <%= image_tag (pi.product_image(:medium)) %>
  <% end %>

我正在寻找的是像这样的输出

<div class="col-md-3 col-sm-6 hero-feature">
      <div class="thumbnail">
        <#PRODUCT_IMAGE product_id1> 
        <div class="caption">
          <h3>#PRODUCT_NAME product_id1</h3> 
          <p>#PRODUCT_DESCRIPTION product_id1</p> 
          <p>
          </p>
        </div>
      </div>
    </div>
 <div class="col-md-3 col-sm-6 hero-feature">
          <div class="thumbnail">
            <#PRODUCT_IMAGE product_id2> 
            <div class="caption">
              <h3>#PRODUCT_NAME product_id2</h3> 
              <p>#PRODUCT_DESCRIPTION product_id2</p> 
              <p>
              </p>
            </div>
          </div>
        </div>
 ...etc

回答SQL输出;干杯苏斯洛夫

 Product Load (0.0ms)  SELECT  `products`.* FROM `products`   ORDER BY `products`.`id` DESC LIMIT 5
  SQL (1.0ms)  SELECT  DISTINCT `products`.`id` FROM `products` LEFT OUTER JOIN `product_images` ON `product_images`.`product_id` = `products`.`id` WHERE `product_images`.`product_id` IN (1, 2) AND `product_images`.`default_image` = 1  ORDER BY `products`.`id` DESC LIMIT 5
  SQL (0.0ms)  SELECT `products`.`id` AS t0_r0, `products`.`product_name` AS t0_r1, `products`.`product_description` AS t0_r2, `products`.`product_type_id` AS t0_r3, `products`.`product_category_id` AS t0_r4, `products`.`product_colour_id` AS t0_r5, `products`.`product_size_id` AS t0_r6, `products`.`created_at` AS t0_r7, `products`.`updated_at` AS t0_r8, `product_images`.`product_id` AS t1_r0, `product_images`.`created_at` AS t1_r1, `product_images`.`updated_at` AS t1_r2, `product_images`.`id` AS t1_r3, `product_images`.`product_image_file_name` AS t1_r4, `product_images`.`product_image_content_type` AS t1_r5, `product_images`.`product_image_file_size` AS t1_r6, `product_images`.`product_image_updated_at` AS t1_r7, `product_images`.`default_image` AS t1_r8 FROM `products` LEFT OUTER JOIN `product_images` ON `product_images`.`product_id` = `products`.`id` WHERE `product_images`.`product_id` IN (1, 2) AND `product_images`.`default_image` = 1 AND `products`.`id` IN (2, 1)  ORDER BY `products`.`id` DESC
  Rendered home/index.html.erb within layouts/application (0.0ms)

更新了index.htmml.erb部分来自suslov的答案

 <div class="row text-center">
      <% @product_images.each do |pd| %>
          <%= content_tag :div, :class => "col-md-3 col-sm-6 hero-feature" do %>
          <% pd.product_images.each do |i| %>
              <div class="thumbnail">
                <%= image_tag (i.product_image(:medium)) %>
          <% end %> </div>
          <div class="caption">
            <h3><%= pd.product_name %></h3>
            <p><%= pd.id %></p>
            <p></p>
          </div>
          <% end %>
      <% end %>
    </div>

1 个答案:

答案 0 :(得分:2)

如果您在includes模型中明确设置has_many关联(假设名称为Product),则可以尝试使用product_images

应用程序/控制器/ home_controller.rb:

@products = Product.includes(:product_images)
                   .where(product_images: { default_image: true )
                   .last(5)

应用程序/视图/家/ index.html.erb:

<div class="col-md-3 col-sm-6 hero-feature">
  <div class="thumbnail">
    <% @products.each do |pd| %>
        <% pd.product_images.each do |i| %>
            <%= image_tag (i.product_image(:medium)) %>
        <% end %>
    <div class="caption">
      <h3><%= pd.product_name %></h3> 
      <p><%= pd.id %></p> 
      <p></p>
    </div>
    <% end %>
  </div>
</div>