{"id":227,"date":"2022-08-30T14:57:50","date_gmt":"2022-08-30T14:57:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/08\/31\/create-new-record-gets-nameerror-undefined-local-variable-or-method-through-record-and-share-programming-errors\/"},"modified":"2022-08-30T14:57:50","modified_gmt":"2022-08-30T14:57:50","slug":"create-new-record-gets-nameerror-undefined-local-variable-or-method-through-record-and-share-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/create-new-record-gets-nameerror-undefined-local-variable-or-method-through-record-and-share-programming-errors\/","title":{"rendered":"Create new record gets NameError: undefined local variable or method &#39;through&#39;-Record and share programming errors"},"content":{"rendered":"<p>I&#8217;m new to Rails and am following Railscast #258 to implement jQuery TokenInput, and for some reason in trying to create a new record I&#8217;m getting the error:<\/p>\n<pre><code>NameError: undefined local variable or method `through' for #\n    from \/Library\/Ruby\/Gems\/1.8\/gems\/activerecord-3.0.5\/lib\/active_record\/base.rb:1008:in `method_missing'\n    from \/Users\/Travis\/Desktop\/YourTurn\/app\/models\/tag.rb:4\n    from \/Library\/Ruby\/Gems\/1.8\/gems\/activesupport-3.0.5\/lib\/active_support\/dependencies.rb:454:in `load'\n    from \/Library\/Ruby\/Gems\/1.8\/gems\/activesupport-3.0.5\/lib\/active_support\/dependencies.rb:454:in `load_file'\n    from \/Library\/Ruby\/Gems\/1.8\/gems\/activesupport-3.0.5\/lib\/active_support\/dependencies.rb:596:in `new_constants_in'\n    from \/Library\/Ruby\/Gems\/1.8\/gems\/activesupport-3.0.5\/lib\/active_support\/dependencies.rb:453:in `load_file'\n    from \/Library\/Ruby\/Gems\/1.8\/gems\/activesupport-3.0.5\/lib\/active_support\/dependencies.rb:340:in `require_or_load'\n    from \/Library\/Ruby\/Gems\/1.8\/gems\/activesupport-3.0.5\/lib\/active_support\/dependencies.rb:491:in `load_missing_constant'\n    from \/Library\/Ruby\/Gems\/1.8\/gems\/activesupport-3.0.5\/lib\/active_support\/dependencies.rb:183:in `const_missing'\n    from \/Library\/Ruby\/Gems\/1.8\/gems\/activesupport-3.0.5\/lib\/active_support\/dependencies.rb:181:in `each'\n    from \/Library\/Ruby\/Gems\/1.8\/gems\/activesupport-3.0.5\/lib\/active_support\/dependencies.rb:181:in `const_missing'\n<\/code><\/pre>\n<p><strong>My Tags controller:<\/strong><\/p>\n<pre><code>class TagsController &lt; ApplicationController\n\ndef index\n    @tags = Tag.where(\"name like ?\", \"%#{params[:q]}%\")\n    respond_to do |format|\n      format.html\n      format.json { render :json =&gt; @tags.map(&amp;:attributes) }\n    end\n  end\n\n  def show\n    @tag = Tag.find(params[:id])\n  end\n\n  def new\n    @tag = Tag.new\n  end\n\n  def create\n    @tag = Tag.new(params[:tagging])\n    if @tag.save\n      redirect_to @tag, :notice =&gt; \"Successfully created tag.\"\n    else\n      render :action =&gt; 'new'\n    end\n  end\n<\/code><\/pre>\n<p><strong>Tags Method:<\/strong><\/p>\n<pre><code>class Tag &lt; ActiveRecord::Base\n  attr_accessible :name\n  has_many :taggings\n  has_many :questions, through =&gt; :taggings\nend\n<\/code><\/pre>\n<p><strong>Taggings Method:<\/strong><\/p>\n<pre><code>class Tagging &lt; ActiveRecord::Base\n  attr_accessible :question_id, :tag_id\n  belongs_to :question\n  belongs_to :tag\nend\n<\/code><\/pre>\n<p><strong>Method with :through:<\/strong><\/p>\n<pre><code>  has_many :taggings\n  has_many :tags, :through =&gt; :taggings\n  attr_reader :tag_tokens\n<\/code><\/pre>\n<p>If I&#8217;m in Terminal and create <code>tag = Tagging.new<\/code> I get the appropriate entries but not the tag name I have in db migrate <code>create_tags<\/code>. Can anyone help me figure out what the issue is? If I need to provide other code I&#8217;m happy to.<\/p>\n<ol>\n<li>\n<p>You&#8217;re missing a colon, this:<\/p>\n<pre><code>class Tag &lt; ActiveRecord::Base\n  attr_accessible :name\n  has_many :taggings\n  has_many :questions, through =&gt; :taggings\nend\n<\/code><\/pre>\n<p>should be this:<\/p>\n<pre><code>class Tag &lt; ActiveRecord::Base\n  attr_accessible :name\n  has_many :taggings\n  has_many :questions, :through =&gt; :taggings\nend\n<\/code><\/pre>\n<p>Note that the <code>through<\/code> changed to <code>:through<\/code>. Just <code>through<\/code> is a variable but <code>:through<\/code> is a symbol and that&#8217;s what you usually want for building hashes.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-08-31 05:11:06. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m new to Rails and am following Railscast #258 to implement jQuery TokenInput, and for some reason in trying to create a new record I&#8217;m getting the error: NameError: undefined local variable or method `through&#8217; for # from \/Library\/Ruby\/Gems\/1.8\/gems\/activerecord-3.0.5\/lib\/active_record\/base.rb:1008:in `method_missing&#8217; from \/Users\/Travis\/Desktop\/YourTurn\/app\/models\/tag.rb:4 from \/Library\/Ruby\/Gems\/1.8\/gems\/activesupport-3.0.5\/lib\/active_support\/dependencies.rb:454:in `load&#8217; from \/Library\/Ruby\/Gems\/1.8\/gems\/activesupport-3.0.5\/lib\/active_support\/dependencies.rb:454:in `load_file&#8217; from \/Library\/Ruby\/Gems\/1.8\/gems\/activesupport-3.0.5\/lib\/active_support\/dependencies.rb:596:in `new_constants_in&#8217; from \/Library\/Ruby\/Gems\/1.8\/gems\/activesupport-3.0.5\/lib\/active_support\/dependencies.rb:453:in `load_file&#8217; from [&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-227","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/227","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=227"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/227\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}