我打電話給一組推薦產品(基於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
:不是在陣列
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關於如何解決此問題的任何想法? :) –