Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Not an answer, just a test.</strong></p> <p>First, reduce the data extracting keywords for each poem as they are saved:</p> <pre><code>rails g resource Keyword word occurrences poem_id:integer rails db:migrate </code></pre> <p>Then in your Poem model:</p> <pre><code># add more words EXCLUDED_WORDS = %w( the a an so that this these those ) has_many :keywords before_save :set_keywords # { :some =&gt; 3, :word =&gt; 2, :another =&gt; 1} def keywords_hash(how_many = 5) words = Hash.new 0 poem_column.split.each do |word| words[word] += 1 if not word.in? EXCLUDED_WORDS end Hash[words.sort { |w, w1| w1 &lt;=&gt; w }.take(how_many)] end def set_keywords keywords_hash.each do | word, occurrences | keywords.create :word =&gt; word, :occurrences =&gt; occurrences end end </code></pre> <p>In <code>Keyword</code> model:</p> <pre><code>belongs_to :poem def self.poem_ids includes(:poem).map(&amp;:poem_id) end def self.poems Poem.where(id: poem_ids) end </code></pre> <p>Then when you have word to search for:</p> <pre><code>keywords = Keyword.where(word: word) poems = keywords.poems poets = poems.poets </code></pre> <p>To use this last part, you would need in <code>Poem</code> model:</p> <pre><code>def self.poet_ids includes(:poet).map(&amp;:poet_id) end def self.poets Poet.where(id: poet_ids) end </code></pre> <p>As far as I see this way would require just 3 queries, no joins, so it seems to make sense.</p> <p>I will think in how to extend this way to search by the entire content.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload