{"id":669,"date":"2022-08-30T15:05:12","date_gmt":"2022-08-30T15:05:12","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/get-the-value-of-has_many-in-rails-errorno-method-error-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:12","modified_gmt":"2022-08-30T15:05:12","slug":"get-the-value-of-has_many-in-rails-errorno-method-error-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/get-the-value-of-has_many-in-rails-errorno-method-error-collection-of-common-programming-errors\/","title":{"rendered":"Get the value of has_many in rails error(no method error)-Collection of common programming errors"},"content":{"rendered":"<p>The problem is that at least one of the <code>comments<\/code> on your <code>post<\/code> object has a <code>nil<\/code> value for the <code>user<\/code> association. You&#8217;re checking that <code>email<\/code> is not nil, but you&#8217;re not checking whether <code>user<\/code> itself is nil (which is what is triggering the <code>NoMethodError<\/code>).<\/p>\n<p>As a start, I would change this line:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>to:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>This is a handy pattern in ruby which first checks that <code>comment.user<\/code> is defined, and if it is defined returns the second argument, i.e. <code>comment.user.email<\/code>. If <code>comment.user<\/code> is not defined (or <code>nil<\/code>, or <code>false<\/code>) then the second argument is not evaluated, and the return value is nil (so if no user is defined, then <code>comment.user.email<\/code> is never evaluated so you don&#8217;t get an error).<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:20:56. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>The problem is that at least one of the comments on your post object has a nil value for the user association. You&#8217;re checking that email is not nil, but you&#8217;re not checking whether user itself is nil (which is what is triggering the NoMethodError). As a start, I would change this line: to: This [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-669","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=669"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/669\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}