ruby on rails - Display nested attributes together in view or index.html.erb -
i have product model of attributes , has_many relationship through product_images model. of updates, edits etc work charm i'm having difficulty bringing data through front end bootstrap theme.
what i'm trying have last 5 products , there default images come through can have product image , associated product attributes together. @ present have attributes , of images in 2 separate arrays.
i have looked @ group_by cant work out how have attributes + images grouped 2 arrays product_id. have no idea how go this.
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
the below dumps 2 arrays not together.image / attributes per product_id
app/views/home/index.html.erb
<% @products.each |pd| %> <%= pd.product_name %> <% end %> <% @product_images.each |pi| %> <%= image_tag (pi.product_image(:medium)) %> <% end %>
what i'm looking output this
<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
answer sql output; cheers suslov
product load (0.0ms) select `products`.* `products` order `products`.`id` desc limit 5 sql (1.0ms) select distinct `products`.`id` `products` left outer join `product_images` on `product_images`.`product_id` = `products`.`id` `product_images`.`product_id` in (1, 2) , `product_images`.`default_image` = 1 order `products`.`id` desc limit 5 sql (0.0ms) select `products`.`id` t0_r0, `products`.`product_name` t0_r1, `products`.`product_description` t0_r2, `products`.`product_type_id` t0_r3, `products`.`product_category_id` t0_r4, `products`.`product_colour_id` t0_r5, `products`.`product_size_id` t0_r6, `products`.`created_at` t0_r7, `products`.`updated_at` t0_r8, `product_images`.`product_id` t1_r0, `product_images`.`created_at` t1_r1, `product_images`.`updated_at` t1_r2, `product_images`.`id` t1_r3, `product_images`.`product_image_file_name` t1_r4, `product_images`.`product_image_content_type` t1_r5, `product_images`.`product_image_file_size` t1_r6, `product_images`.`product_image_updated_at` t1_r7, `product_images`.`default_image` t1_r8 `products` left outer join `product_images` on `product_images`.`product_id` = `products`.`id` `product_images`.`product_id` in (1, 2) , `product_images`.`default_image` = 1 , `products`.`id` in (2, 1) order `products`.`id` desc rendered home/index.html.erb within layouts/application (0.0ms)
updated index.htmml.erb partial answer suslov
<div class="row text-center"> <% @product_images.each |pd| %> <%= content_tag :div, :class => "col-md-3 col-sm-6 hero-feature" %> <% pd.product_images.each |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>
you can try includes
if have explicitly set has_many
association in product
model (assuming is named 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 |pd| %> <% pd.product_images.each |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>
Comments
Post a Comment