{"id":1116,"date":"2022-08-30T15:12:39","date_gmt":"2022-08-30T15:12:39","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/has_many-through-breaks-upon-find_or_initialize_by-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:12:39","modified_gmt":"2022-08-30T15:12:39","slug":"has_many-through-breaks-upon-find_or_initialize_by-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/has_many-through-breaks-upon-find_or_initialize_by-collection-of-common-programming-errors\/","title":{"rendered":"has_many :through breaks upon find_or_initialize_by-Collection of common programming errors"},"content":{"rendered":"<p>I have a model, Feed, that has and belongs to many FilteredUsers. In this case I have implemented it through a has_many :through relationship.<\/p>\n<pre><code>class Feed &lt; ActiveRecord::Base\n  has_many :denials, :dependent =&gt; :destroy\n  has_many :filtered_users, :through =&gt; :denials\n<\/code><\/pre>\n<p>I would like to create a record if it doesn&#8217;t exist or find the object if it does exist. When I try and use the find_or_initialize_by (or find_or_create_by) an exception is thrown saying <code>undefined method 'feed_id=' for params[:user_url]) else feed.filtered_users.find_by_user_url(params[:user_url]).delete end feed.save<\/code><\/p>\n<p><strong>warning this is air coded not tested<\/strong><\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:30:01. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a model, Feed, that has and belongs to many FilteredUsers. In this case I have implemented it through a has_many :through relationship. class Feed &lt; ActiveRecord::Base has_many :denials, :dependent =&gt; :destroy has_many :filtered_users, :through =&gt; :denials I would like to create a record if it doesn&#8217;t exist or find the object if it [&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-1116","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1116","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=1116"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1116\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}