私は推奨製品(predictor-gem
に基づいて)の配列を呼び出し、そのセットからcurrent_user
の製品を除外したいと思います。私はそれが正しい方法ではない(" != ?")
のための適切な条件を使用していると思う。 @products_rec
はその最終配列を与えるべきです。findメソッドからidの配列を除外する方法は?
ここでは具体的な私のproduct_controller.rb
のコードです:
、ここでは私のモデルだ、product.rb:
class Product < ActiveRecord::Base
include Reviewing
include PublicActivity::Model
tracked
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]
belongs_to :category
belongs_to :benefit
has_many :subscriptions
has_many :users, through: :subscriptions
has_many :benefits
has_many :projects, through: :benefits
belongs_to :user
validates :name, presence: true, length: { maximum: 200 }
validates :gtin, presence: false
validates :content, presence: false, length: { maximum: 2000 }
validates :video, presence: false
validates :tag, presence: false
validates :project, presence: false
validates :category, presence: false
validates :user, presence: false
has_attached_file :image, :styles => { :medium => "680x300>", :thumb => "170x75>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
私はを通じてユーザーに基づいて、current_user
から製品を除外したいが、サブスクリプション(モデルを参照)。
これをどのように動作させることができますか、どのようなアイデアですか?
決勝コード:@Humza
の回答に基づいて 、私は私のproduct_controller.rb
に次の作業コードを追加しました:いない配列内のIDを持つ製品を検索するに
recommender = ProductRecommender.new
products = current_user.products.select("id")
@product_rec = (recommender.similarities_for("product-#{@product.id}")).map {|el| el.gsub(/(.*\-)[^\d]*/, "")}
@products_rec_array = Product.find(@product_rec)
@products_rec = Product.where(id: @products_rec_array).where.not(id: products)
使用しているRailsのバージョンは? – Pavan
私はRails 4を使用しています –
@Pavanどのように私はこれを解決することができますか? :) –