2017-03-15 5 views
1

カテゴリを使用してエントリをフィルタリングするにはどうすればよいですか?カテゴリ別のフィルタリング方法...スコープまたは別の方法ですか?

私は、教育的目的のためにredditを題材にしており、タイトル、リンク、カテゴリのみを削っています。ここで

は、libディレクトリを経由して、それのためのコードです:

require 'open-uri' 

module RedditScrapper 
    def self.scrape 
    doc = Nokogiri::HTML(open("https://www.reddit.com/")) 

    entries = doc.css('.entry') 
    entries.each do |entry| 
     title = entry.css('p.title > a').text 
     link = entry.css('p.title > a')[0]['href'] 
     category = entry.css('p.tagline > a.subreddit')[0]['href'] 
     Entry.create!(title: title, link: link, category: category) 
    end 
    end 

end 

index.html.erbではなく、どのように損失でこれをフィルタリングしようとしています。

私は以前に書いたことがないのに、スコープを試してみることに決めました。ここで

Entryモデル

class Entry < ApplicationRecord 

    validates :title, presence: true 
    validates :link, presence: true 
    validates :category, presence: true 

    scope :categorization, -> (category) { where category: category } 

end 

内のスコープは述べ、このエラーメッセージが表示されentries_controller

class EntriesController < ApplicationController 


def index 
    @entries = Entry.all 
    @entries = @entries.categorization 
end 

def scrape 

    RedditScrapper.scrape 

    respond_to do |format| 
     format.html { redirect_to entries_url, notice: 'Entries were successfully scraped.' } 
     format.json { entriesArray.to_json } 
    end 
    end 

end 

にindexアクションにスコープを入れてあります。

ArgumentError in EntriesController#index 
wrong number of arguments (given 0, expected 1) 

を、それが参照してい範囲に。

これを解決するにはどうすればよいですか、スコープではなく別の方法がある場合は、カテゴリ別にフィルタリングしてください。

ありがとうございます。追加データが必要な場合はお知らせください。あなたがここにスコープを必要としない出力の

サンプル

LifeProTips 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

答えて

1

、彼らはwhere句をクリーンアップするためのものです。

{ 
    "category1" => [Entry,Entry,Entry], 
    "category2" => [Entry], 
    ... 
} 

は、あなたのビューであなたが

<% @entries.each do |category, entries| %> 
    <h1><%= category %></h1> 
    <% entries.each do |entry| %> 
    <p><%= entry.inspect %></p> 
    <% end %> 
<% end %> 

あなたの第二の問題は、スクレーパーで重複を処理していないことができます:

だけ group_by

このようなハッシュを作成します
def index 
    @entries = Entry.all.group_by{ |entry| entry.category } 
    # a shorter more ruby way of writing this is: 
    # @entries = Entry.all.group_by(&:category) 
end 

を使用

変更:

Entry.create!(title: title, link: link, category: category) 

Entry.where(title: title, link: link, category: category).first_or_create 
+0

は、それは一種の...ビットは出力が自分自身を繰り返し続けることを困惑作品...あなたの答えをありがとうございました...上記の記事でサンプルを掲載しています。..どんな理論? – user273072545345

+0

また、すべてのクリック可能なカテゴリが列挙されている可能性があるのだろうかと疑問に思っていたので、クリックするとその項目だけが表示されるように並べ替えられます。 – user273072545345

+0

興味深い...私はもう一度見ました...それはそれを複製するだけではないことが判明しました...文字通りデータベース内の別々のエントリです...私は2分ごとにスクレーパーを設定するので、それを繰り返し続けますよね?なぜなら、私が 'uniq'を試したときでも...それでも解決されませんでした...インデックスアクション '@lala = @ entries.uniq.group_by(&:category)' – user273072545345

関連する問題