简体   繁体   English

在ruby / rails中的哈希中过滤一组哈希

[英]Filter an array of hashes within a hash in ruby/rails

I'm aggregating RSS feeds using FeedTools. 我正在使用FeedTools聚合RSS feed。 I followed this tutorial: http://simonwoodside.com/weblog/2008/12/7/ruby_on_rails_feedrss_aggregator/ and everything functioned fine. 我遵循了本教程: http : //simonwoodside.com/weblog/2008/12/7/ruby_on_rails_feedrss_aggregator/ ,一切正常。

The problem is I want to filter some of the hashes that exist within the array [:items] from inside the parsed feed hash. 问题是我想从解析的Feed哈希中过滤掉数组[:items]中存在的某些哈希。

Here's the RSS cached feed object in my database: 这是我数据库中的RSS缓存的提要对象:

  create_table "cached_feeds", :force => true do |t|
    t.string   "uri"
    t.text     "parsed_feed"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

In script/console the parsed feed looks something like this: 在脚本/控制台中,已解析的提要看起来像这样:

parsed_feed: {:uri => "http://www.someblog.com",
:items =>
[ {:published_at => some date,
  :link => "http://www.someblog.com/somelink.html",
  :title => "Some Title"},

  {:published_at => another date,
  :link => "http://www.someblog.com/anotherlink.html",
  :title => "Another Title"} ]
}

As you can see there are two hashes for parsed_feed, :uri and :items. 如您所见,parsed_feed有两个哈希::uri和:items。 :items has all the feed item details stashed in a big array of hashes. :items将所有提要项的详细信息隐藏在大量哈希中。

Now lets say that "Some Title" is already the name of an article in the database. 现在说“ Some Title”已经是数据库中文章的名称。 The purpose of the RSS feed list of items is to tell the user which articles in their RSS feed haven't been uploaded into the db yet, so I only want "Another Title" to show in the view. RSS提要项列表的目的是告诉用户尚未将其RSS提要中的哪些文章上载到数据库中,因此我只希望在视图中显示“另一个标题”。

This method in the controller does a good job showing all the items without a filter: 控制器中的此方法可以很好地显示没有过滤器的所有项目:

def read_cache
    website_articles = @website.article_names.to_a #This would return ["Some Title"]
    @@uris = ["#{Website.find(params[:id]).feed}"]
    @@uris.map { |uri|
    feed = CachedFeed.find_by_uri( uri ).parsed_feed
    feed[:items].map { |item| {:feed_title => @@title_map[feed[:title]] || feed[:title],   :feed_item => item} }
    } .flatten.sort_by { |item| item[:feed_item][:published] } .reverse
  end

I tried to insert this code after .flatten as a filter 我试图在.flatten之后插入此代码作为过滤器

.reject {|k,v| website_articles.include? v}

This would work if I was filtering a normal hash, but filtering from an array of hashes within a hash. 如果我正在过滤普通哈希,但从哈希中的哈希数组进行过滤,则此方法有效。 What am I missing? 我想念什么? How can I get "Another Title" to appear in the view without "Some Title" appearing? 如何使“另一个标题”出现在视图中而没有出现“某些标题”?

由于拒绝中的v{ :feed_title => "Some Title", ... }类的哈希,因此它应该起作用:

.reject {|k, v| website_articles.include? v[:feed_title] }

@Jordan @约旦

I got a nil back when I did that. 当我这样做的时候,我得到了零。 However, your answer did get me on the right track. 但是,您的回答确实使我步入正轨。

Did a little tinkering and apparently I didn't need two variables for the hash: 做了一些修改,显然我不需要两个变量来进行哈希处理:

.reject {|item| website_articles.include? item[:feed_item][:title]}

Thanks for getting me on the right track! 感谢您使我步入正轨!

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

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