在视图中一起显示嵌套属性或 index.html.erb
Display nested attributes together in view or index.html.erb
我有一个 Product 模型,其中大部分属性和 has_many 关系一直到我的 Product_Images 模型。所有的更新、编辑等都非常有效,但我很难将数据传送到前端 bootstrap 主题。
我想要做的是获取最后 5 个产品,并显示默认图片,这样我就可以获得 产品图片 和关联产品 Attributes 在一起。目前,我在 2 个单独的数组中拥有所有属性和所有图像。
我看过 group_by,但我不知道如何通过 product_id 从两个数组中将属性 + 图像组合在一起。我只是不知道该怎么做。
app/controller/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
下面基本上转储了两个数组,但它们不是 together.Image / product_id
的属性
app/views/home/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)
根据 suslov
的回答更新了 index.htmml.erb 部分
<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>
如果您在 Product
模型中明确设置了 has_many
关联,则可以尝试使用 includes
(假设名为 product_images
):
app/controller/home_controller.rb:
@products = Product.includes(:product_images)
.where(product_images: { default_image: true )
.last(5)
app/views/home/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>
我有一个 Product 模型,其中大部分属性和 has_many 关系一直到我的 Product_Images 模型。所有的更新、编辑等都非常有效,但我很难将数据传送到前端 bootstrap 主题。
我想要做的是获取最后 5 个产品,并显示默认图片,这样我就可以获得 产品图片 和关联产品 Attributes 在一起。目前,我在 2 个单独的数组中拥有所有属性和所有图像。
我看过 group_by,但我不知道如何通过 product_id 从两个数组中将属性 + 图像组合在一起。我只是不知道该怎么做。
app/controller/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
下面基本上转储了两个数组,但它们不是 together.Image / product_id
的属性app/views/home/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)
根据 suslov
的回答更新了 index.htmml.erb 部分 <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>
如果您在 Product
模型中明确设置了 has_many
关联,则可以尝试使用 includes
(假设名为 product_images
):
app/controller/home_controller.rb:
@products = Product.includes(:product_images)
.where(product_images: { default_image: true )
.last(5)
app/views/home/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>