简体   繁体   English

根据兴趣向用户推荐产品的最佳方式?

[英]Best way to recommend Products to Users based on Interest?

Let's say that each Product has a category. 假设每个产品都有一个类别。 I want to ask the Users to select several categories that the user is interested in, and find the Products that have the same category. 我想让用户选择用户感兴趣的几个类别,并找到具有相同类别的产品。 This is similar to what Quora, Stumbleupon, and Pinterest all do. 这类似于Quora,Stumbleupon和Pinterest所做的一切。

What would be the best way to set this database structure in Rails? 在Rails中设置此数据库结构的最佳方法是什么? Should I create 3 tables: User, Product, and Category, and make the relations User has many Categories & Product has many Categories? 我应该创建3个表:用户,产品和类别,并建立关系用户有很多类别和产品有很多类别?

The problem I see with this is doesn't it create, rather than reference, a new instance of Categories to each row of Users and Products? 我看到的问题是它不是为每行用户和产品创建而不是引用类别的新实例?

*extra: What if I wanted subcategories? *额外:如果我想要子类别怎么办? For example, if the user chose Technology, it could further ask to choose between web dev, mobile dev, hardware, etc. 例如,如果用户选择了技术,则可以进一步要求在web开发,移动开发,硬件等之间进行选择。

You could do that kind of 'recommendation' pretty easily. 你可以很容易地做那种“推荐”。

Something like this should work (NB: I did not test this code, but it is right in spirit): 这样的东西应该有效(注意:我没有测试这段代码,但它在精神上是正确的):

def recommended_products
  joins(:categories, :products).where("product_id not in (?)", self.products)
end

Explanation of each bit: 每个位的说明:

joins(:categories, :products) : this does a SQL join of users, products, and categories. joins(:categories, :products) :这是一个用户,产品和类别的SQL连接。 This gives you a 'table' where each user-product-category combination is in it's own row. 这为您提供了一个“表格”,其中每个用户 - 产品 - 类别组合都在其自己的行中。

.where("product_id not in (?)", self.products) : adds a SQL where clause to filter out all the rows that have products in the current user's list of products. .where("product_id not in (?)", self.products) :添加一个SQL where子句,以过滤掉当前用户产品列表中包含产品的所有行。

The associations are not a problem. 这种关联不是问题。 They don't create any new instances by themselves, only if you write code that creates new instances yourself. 只有在您编写自己创建新实例的代码时,它们才会自行创建任何新实例。

As for sub categories, I think you'll do better to make that it's own question, as it's easily a whole post in itself. 至于子类别,我认为你做得更好,这是自己的问题,因为它本身很容易成为一个完整的帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM