Rails使用某些标签查找所有帖子,而不使用acts_as_taggable [英] Rails finding all posts with certain tags without using acts_as_taggable

查看:311
本文介绍了Rails使用某些标签查找所有帖子,而不使用acts_as_taggable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图学习rails,我不想作弊。



post模型:

  class Post< ActiveRecord :: Base 
has_many:features
has_many:tags,:through => :功能
结束

标记模型

  class Tag< ActiveRecord :: Base 
has_many:features
has_many:posts,:through => :功能
结束

连接表

  class Feature< ActiveRecord :: Base 
belongs_to:tag
belongs_to:post
end



<我已经知道如何通过下面的方式将帖子与标签相关联: Post.find_by_id(1)<< Tag.first



现在,我一直在搜索某些标签的帖子。如何搜索所有包含以下一个或多个标签的帖子:游泳正在运行赚钱

< Post1包含标签:骑自行车,攀岩,游泳。

Post2包含标签:青蛙,鱼
$ b Post3包含标签:赚钱,游泳,骑自行车,爱

Post4包含标签:游泳



我希望与用户兴趣最匹配的帖子能够最先显示出来。

例子:user应该看到这个顺序的职位列表.... post3,post1,post4。如果这太难了,找到所有具有确切标签的帖子的方法就足够了。我猜。 解决方案

你应该是能够做到的

$ $ p $ $ $ $ c $ @ tag.posts

是否有任何理由不使用 has_and_belongs_to_many 关系?



更新:



根据您的评论:

  tag_ids = [1,2,3]#aka the标签[游泳,跑步,赚钱]的ID 
匹配= {}
@ posts.each do | post |
count = 0;
post.tags.each do | tag |
count + = 1如果tag_ids.include? tag.id
结束
匹配[count] || = []
匹配[count]<< post
end

可能会优化一下,但匹配然后将根据匹配的数量在键中包含与请求标签匹配的所有帖子。这应该也可能在Posts模型的类方法中,而不是在控制器中。


So I'm trying to learn rails and I don't want to cheat just yet.

post model:

class Post < ActiveRecord::Base
    has_many :features
    has_many :tags, :through => :features
end

tag model:

class Tag < ActiveRecord::Base
    has_many :features
    has_many :posts, :through => :features
end

join table:

class Feature < ActiveRecord::Base
    belongs_to :tag
    belongs_to :post
end

I already know how to associate posts with tags by doing: Post.find_by_id(1) << Tag.first

Now, I'm stuck on searching for posts with certain tags. How do I search for all posts that have one or more of the following tags: "swimming", "running", "making money".

Post1 includes tags: "biking", "rock climbing", "swimming"

Post2 includes tags: "frogs", "fish"

Post3 includes tags: "making money", "swimming", "biking", "love"

Post4 includes tags: "swimming"

I want the posts that matches the user's interests the most to show up first.

example: user should see a list of posts in this order.... post3, post1, post4. If this is too hard, a method of finding all posts with the exact tags will suffice I guess.

解决方案

You should just be able to do

@tag.posts

Is there any reason you didn't use the has_and_belongs_to_many relation?

UPDATE:

Per your comment:

tag_ids = [1, 2, 3] # aka the IDs for tags ["swimming", "running", "making money"]
matches = {}
@posts.each do |post|
  count = 0;
  post.tags.each do |tag|
    count += 1 if tag_ids.include? tag.id
  end
  matches[count] ||= []
  matches[count] << post
end

Could probably be optimized a bit, but matches will then include all the posts matching the requested tag in keys according to the number of matches. This should also probably go in a class method for the Posts model instead of in the controller.

这篇关于Rails使用某些标签查找所有帖子,而不使用acts_as_taggable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆