{"id":581,"date":"2022-08-30T15:03:44","date_gmt":"2022-08-30T15:03:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/undefined-method-new_record-for-nilnilclass-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:03:44","modified_gmt":"2022-08-30T15:03:44","slug":"undefined-method-new_record-for-nilnilclass-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/undefined-method-new_record-for-nilnilclass-collection-of-common-programming-errors\/","title":{"rendered":"undefined method `new_record?&#39; for nil:NilClass-Collection of common programming errors"},"content":{"rendered":"<p>In rails 3.2 I created a post controller. Each post can have a different number of paperclip attachments. To achieve this I created a assets model where each asset has a paperclip attachment. One post has_many assets and assets belong_to post.<\/p>\n<p>Asset model<\/p>\n<pre><code>class Asset &lt; ActiveRecord::Base\n    belongs_to :post\n    has_attached_file :photo, :styles =&gt; { :thumb =&gt; \"200x200&gt;\" }\nend\n<\/code><\/pre>\n<p>Post model<\/p>\n<pre><code> class Post &lt; ActiveRecord::Base\n      attr_accessible :content, :title\n      has_many :assets, :dependent =&gt; :destroy\n      validates_associated :assets\n      after_update :save_assets\n\n def new_asset_attributes=(asset_attributes) \n        asset_attributes.each do |attributes| \n            assets.build(attributes) \n        end \n    end\n    def existing_asset_attributes=(asset_attributes) \n        assets.reject(&amp;:new_record?).each do |asset| \n        attributes = asset_attributes[asset.id.to_s] \n        if attributes \n            asset.attributes = attributes \n            else \n                asset.delete(asset) \n            end \n        end \n    end\n\n    def save_assets \n        assets.each do |asset| \n            asset.save(false) \n        end \n    end \nend\n<\/code><\/pre>\n<p>Posts helper<\/p>\n<pre><code>module PostsHelper\n  def add_asset_link(name) \n    link_to_function name do |post| \n      post.insert_html :bottom, :assets, :partial =&gt; 'asset', :object =&gt; Asset.new \n    end \n  end\nend\n<\/code><\/pre>\n<p>Form for post<\/p>\n<pre><code> { :multipart =&gt; true } do |f| %&gt;\n  \n    \n      <\/code><\/pre>\n<h2><code>prohibited this post from being saved:<\/code><\/h2>\n<pre>\n\n      <\/pre>\n<pre>\n    \n  \n\n  \n    <br \/>\n    \n  \n  \n    <br \/>\n    \n  \n    \n        Attach a file or image<br \/>\n         @post.assets %&gt; \n       \n  \n    \n  \n\n<\/pre>\n<p><code>Asset partial<\/code><\/p>\n<pre><code><code> \n    \n    \n\n     \n        <br \/> \n          Asset:  \n           \n        <br \/><br \/> \n   \n\n<\/code><\/code><\/pre>\n<p>Most of the code is taken from here: https:\/\/gist.github.com\/33011 and I understand this is a rails2 app, anyway I don&#8217;t understand what this error means:<\/p>\n<pre><code>undefined method `new_record?' for nil:NilClass\nExtracted source (around line #2):\n\n1:  \n2:     \n3:     \n4:     \n5:      \n<\/code><\/pre>\n<ol>\n<li>\n<p>Try changing this<\/p>\n<pre><code> \n    Attach a file or image<br \/>\n     @post.assets %&gt; \n  \n<\/code><\/pre>\n<p>to<\/p>\n<pre><code>\n  Attach a file or image<br \/>\n  \n     asset %&gt; \n  \n\n<\/code><\/pre>\n<p>Possible reason is when u calling<\/p>\n<pre><code> @post.assets %&gt; \n<\/code><\/pre>\n<p>You are passing a collection to the rendered partial but you are no where using ur collection passed by the name collection in the partial. you are using a variable <strong>Assest<\/strong> which doesnt exist in scope of that particular rendered partial \ud83d\ude42<\/p>\n<\/li>\n<li>\n<p>Reading the error, your <code>@post.assets %&gt;<\/code> line calls an empty array maybe? In your console, can you find @post.assets and is it nil there?<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:05:57. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>In rails 3.2 I created a post controller. Each post can have a different number of paperclip attachments. To achieve this I created a assets model where each asset has a paperclip attachment. One post has_many assets and assets belong_to post. Asset model class Asset &lt; ActiveRecord::Base belongs_to :post has_attached_file :photo, :styles =&gt; { :thumb [&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-581","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/581","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=581"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/581\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}