Forum: Ruby on Rails HasManyThroughCantAssociateNewRecords Exception

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9113eebe3a0e5eac9473fccb04f77af1?d=identicon&s=25 James Coleman (jcole1989)
on 2009-01-01 20:30
I have a model:
class Video < ActiveRecord::Base
  has_many :video_tags
  has_many :video_types
  has_many :tags, :through => :video_tags, :uniq => :true
  has_many :types, :through => :video_types, :uniq => :true

  def get_tag_list(delimiter)
    tags.collect { |t| t.tag }.join(delimiter)
  end

  def set_tag_list(tag_list, delimiter)
    # doesn't remove old tags
    tag_list.split(',').each do |t|
      tags << Tag.find_or_create_by_tag(t)
    end
  end
end

When I call
@video.set_tag_list(params[:tag_list], ',')
in my controller, it throws the exception - Cannot associate new records
through 'Video#video_tags' on '#'. Both records must have an id in order
to create the has_many :through record associating them.
However, in the script/console, the method call works just fine. What
could be causing this problem?
9113eebe3a0e5eac9473fccb04f77af1?d=identicon&s=25 James Coleman (jcole1989)
on 2009-01-01 21:11
Nevermind. I solved my own problem - that code was correct as I
suspected, I had just missed a simple bug in which ID I was passing into
the find for the video.
This topic is locked and can not be replied to.