您好我有一个网站,其产品属于供应商。在产品展示页面上,我想展示一个也属于这个供应商的横幅。目前我正在展示来自任何供应商的随机横幅,如何将其与产品隔离到同一供应商。
我的模特
class Banner < ApplicationRecord
belongs_to :vendor
end
class Product < ApplicationRecord
enum status: { normal: 0, featured: 1 }
extend FriendlyId
friendly_id :name, use: :slugged
is_impressionable counter_cache: true, :column_name => :views
searchkick
acts_as_taggable
has_many :likes
belongs_to :brand
belongs_to :vendor
belongs_to :category
has_many :collects
has_many :banners, :through => :vendor
end
class Vendor < ApplicationRecord
extend FriendlyId
friendly_id :name, use: :slugged
has_many :products
has_many :brands
has_many :promotions
has_many :banners
end
产品控制器中的节目
def show
@meta_title = meta_title @product.name
@og_properties = {
title: @meta_title,
type: 'website',
image: view_context.image_url(@product.image),
url: product_path,
locale: 'en_AU',
description: @product.description.html_safe,
}
@similar_products = Product.order("RANDOM()").first(4)
@collection = Collection.all
@product = Product.friendly.find(params[:id])
impressionist(@product)
@banner = Banner.order("RANDOM()").first
end
我目前正在show.html.erb页面上调用它
<%= raw(@banner.trackingcode) %>
答案 0 :(得分:0)
如果每个特定供应商的品牌数量不是很大,这将非常适合您的需求:
@banner = @product.vendor.banners.to_a.sample
或:
@banner = @product.vendor.banners.order("RANDOM()").limit(1).first