I have a Products model, and Outfit Model. I want to show only approved Products on my Outfit show view. It's currently showing all products. Products and Outfit are joined by a middle table called outfit_products. The outfit_products table has a boolean approved column with default false. Take a look.
How do i show only approved products in my view?
Outfit.rb
class Outfit < ActiveRecord::Base
belongs_to :user
has_many :outfit_products
has_many :products, through: :outfit_products
end
outfit_product.rb
class OutfitProduct < ActiveRecord::Base
belongs_to :outfit
belongs_to :product
end
product.rb
class Product < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :outfit_products
has_many :outfits, through: :outfit_products
end
Outfit Controller show
def show
@outfit = Outfit.find(params[:id])
end
Outfit show view just the part to do with showing products
<% @outfit.products.each do |product| %>
<div class="box panel panel-default">
<div class="square_item_image">
<%= link_to image_tag(product.image.url(:medium)), product %>
</div>
<div class ="panel-body">
<div class = "itemlistretailer">
<div class="itemlisttitle">
<%= product.title %>
<div class = "itemlistprice">
$<%= product.price %>
</div>
</div>
<div class="item_info">
<%= image_tag product.user.avatar(:thumb) %>
<%= link_to product.user.username, product.user %>
</div>
</div>
</div>
</div>
<% end %>
Here is parts of my schema. Please see outfit
create_table "outfit_products", force: :cascade do |t|
t.integer "product_id"
t.integer "outfit_id"
t.boolean "approved", default: false
t.boolean "boolean", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "outfit_products", ["outfit_id"], name: "index_outfit_products_on_outfit_id"
add_index "outfit_products", ["product_id"], name: "index_outfit_products_on_product_id"
create_table "outfits", force: :cascade do |t|
t.string "outfit_image_file_name"
t.string "outfit_image_content_type"
t.integer "outfit_image_file_size"
t.datetime "outfit_image_updated_at"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "caption"
end
add_index "outfits", ["user_id"], name: "index_outfits_on_user_id"
create_table "products", force: :cascade do |t|
t.string "title"
t.decimal "price"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.integer "category_id"
end
add_index "products", ["user_id", "created_at"], name: "index_products_on_user_id_and_created_at"
add_index "products", ["user_id"], name: "index_products_on_user_id"
end
Extra info with a binding.pry
=> 19: <% binding.pry %>
20: <% @outfit.products.each do |product| %>
21: <div class="box panel panel-default">
22: <div class="square_item_image">
23: <%= link_to image_tag(product.image.url(:medium)), product %>
24: </div>
[1] pry(#<#<Class:0x007fb2dc346480>>)> @outfit.products
Product Load (1.1ms) SELECT "products".* FROM "products" INNER JOIN "outfit_products" ON "products"."id" = "outfit_products"."product_id" WHERE "outfit_products"."outfit_id" = ? [["outfit_id", 18]]
=> [#<Product:0x007fb2e2a34250
id: 32,
title: "Sparkle Shoes",
price: #<BigDecimal:7fb2e2a4f8e8,'0.18E3',9(27)>,
description: "this is test description",
created_at: Thu, 29 Oct 2015 09:14:50 UTC +00:00,
updated_at: Thu, 29 Oct 2015 09:14:50 UTC +00:00,
user_id: 53,
image_file_name: "womenshoes4.jpg",
image_content_type: "image/jpeg",
image_file_size: 86729,
image_updated_at: Thu, 29 Oct 2015 09:14:49 UTC +00:00,
category_id: 20>]
[2] pry(#<#<Class:0x007fb2dc346480>>)> @outfit.products[0].outfit_products
OutfitProduct Load (10.2ms) SELECT "outfit_products".* FROM "outfit_products" WHERE "outfit_products"."product_id" = ? [["product_id", 32]]
=> [#<OutfitProduct:0x007fb2e2b267d0 id: 5, product_id: 32, outfit_id: 17, approved: true, boolean: false, created_at: Thu, 29 Oct 2015 11:43:41 UTC +00:00, updated_at: Thu, 29 Oct 2015 11:46:05 UTC +00:00>,
#<OutfitProduct:0x007fb2e2b26618 id: 7, product_id: 32, outfit_id: 18, approved: true, boolean: false, created_at: Thu, 29 Oct 2015 12:05:20 UTC +00:00, updated_at: Thu, 29 Oct 2015 12:05:20 UTC +00:00>,
#<OutfitProduct:0x007fb2e2b26410 id: 8, product_id: 32, outfit_id: 21, approved: false, boolean: false, created_at: Thu, 29 Oct 2015 12:09:50 UTC +00:00, updated_at: Thu, 29 Oct 2015 12:09:50 UTC +00:00>]
[3] pry(#<#<Class:0x007fb2dc346480>>)>
Aucun commentaire:
Enregistrer un commentaire