Create new record gets NameError: undefined local variable or method 'through'-Record and share programming errors

I’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’m getting the error:

NameError: undefined local variable or method `through' for #
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/base.rb:1008:in `method_missing'
    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'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:454:in `load_file'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:453:in `load_file'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:340:in `require_or_load'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:491:in `load_missing_constant'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:183:in `const_missing'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:181:in `each'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:181:in `const_missing'

My Tags controller:

class TagsController < ApplicationController

def index
    @tags = Tag.where("name like ?", "%#{params[:q]}%")
    respond_to do |format|
      format.html
      format.json { render :json => @tags.map(&:attributes) }
    end
  end

  def show
    @tag = Tag.find(params[:id])
  end

  def new
    @tag = Tag.new
  end

  def create
    @tag = Tag.new(params[:tagging])
    if @tag.save
      redirect_to @tag, :notice => "Successfully created tag."
    else
      render :action => 'new'
    end
  end

Tags Method:

class Tag < ActiveRecord::Base
  attr_accessible :name
  has_many :taggings
  has_many :questions, through => :taggings
end

Taggings Method:

class Tagging < ActiveRecord::Base
  attr_accessible :question_id, :tag_id
  belongs_to :question
  belongs_to :tag
end

Method with :through:

  has_many :taggings
  has_many :tags, :through => :taggings
  attr_reader :tag_tokens

If I’m in Terminal and create tag = Tagging.new I get the appropriate entries but not the tag name I have in db migrate create_tags. Can anyone help me figure out what the issue is? If I need to provide other code I’m happy to.

  1. You’re missing a colon, this:

    class Tag < ActiveRecord::Base
      attr_accessible :name
      has_many :taggings
      has_many :questions, through => :taggings
    end
    

    should be this:

    class Tag < ActiveRecord::Base
      attr_accessible :name
      has_many :taggings
      has_many :questions, :through => :taggings
    end
    

    Note that the through changed to :through. Just through is a variable but :through is a symbol and that’s what you usually want for building hashes.

Originally posted 2013-08-31 05:11:06.