Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation in Model giving undefine nil []
    primarykey
    data
    text
    <p>I have the following code which is running a validation on all snippet content to check the word count against a constant but I keep getting the following error:</p> <pre><code>undefined method `[]' for nil:NilClass Extracted source (around line #36): current_snippets_size = (self.book.get_word_count || 0) + word_count errors.add(:base, "Content size is too big") unless word_count &lt; BOOK_SIZE[book_limit]['per'] &amp;&amp; current_snippets_size &lt; BOOK_SIZE[book_limit]['total'] end </code></pre> <p>Here is the full code from the model (snippet.rb)</p> <pre><code>BOOK_SIZE = { 0 =&gt; {"per" =&gt; 5, "total" =&gt; 50}, 1 =&gt; {"per" =&gt; 6 , "total" =&gt; 60}, 2 =&gt; {"per" =&gt; 7, "total" =&gt; 70} } def size_limit book_limit = self.book.size word_count = self.content.scan(/\w+/).size current_snippets_size = (self.book.get_word_count || 0) + word_count binding.pry errors.add(:base, "Content size is too big") unless word_count &lt; BOOK_SIZE[book_limit]['per'] &amp;&amp; current_snippets_size &lt; BOOK_SIZE[book_limit]['total'] end </code></pre> <p>Because snippets is a child of books, here is the <code>word_count</code> listed in book.rb:</p> <pre><code>def get_word_count @word_count = [] self.snippets.each do |c| @word_count &lt;&lt; c.content.scan(/\w+/).size end @word_count = @word_count.inject(:+) end </code></pre> <p>EDIT: Pry Debug</p> <pre><code> 31: def size_limit =&gt; 32: binding.pry 33: book_limit = self.book.size 34: 35: word_count = self.content.scan(/\w+/).size 36: 37: current_snippets_size = (self.book.get_word_count || 0) + word_count 38: 39: errors.add(:base, "Content size is too big") unless word_count &lt; BOOK_SIZE[book_limit]['per'] &amp;&amp; current_snippets_size &lt; BOOK_SIZE[book_limit]['total'] 40: end [1] pry(#&lt;Snippet&gt;)&gt; p book_limit nil =&gt; nil [2] pry(#&lt;Snippet&gt;)&gt; p word_count nil =&gt; nil [3] pry(#&lt;Snippet&gt;)&gt; errors.any? =&gt; false [4] pry(#&lt;Snippet&gt;)&gt; errors.blank? =&gt; true </code></pre> <p>I tried using PRY but I am still new to the debugging side, some documents describing the best way to walk through PRY debugging would be really useful.</p> <p>Thanks.</p>
    singulars
    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.
 

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