Forum: Ruby on Rails acts_as_taggable: weird SQL problem with untagging

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.
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2006-01-22 02:12
(Received via mailing list)
Setup as follows:

class Resource < ActiveRecord::Base

  belongs_to :user
  validates_presence_of :filename
  validates_uniqueness_of :filename, :scope => "user_id",
    :message => "already exists, try uploading another file or deleting
first."
  acts_as_taggable :join_class_name => 'TagResource'

---------------------------------------------------------------------------------------------------

class TagResource
  belongs_to :user
end

---------------------------------------------------------------------------------------------------

And I'm trying to remove a tag from all files belonging to the current
user:

def delete_tag
    tag = @params[:tag]
    affected = Resource.find_tagged_with({
      :any => tag,
      :conditions => "tags_resources.user_id = #{session[:user].id}"
    })
    affected.each {|res|
      a = res.tag_names - [tag]
      a = ["untagged"] if a.empty?
      res.tag a, :clear => true,
	:attributes => { :user_id => @session[:user].id }
    }
    redirect_to :action => 'list'
  end

---------------------------------------------------------------------------------------------------

which generates the following error:

SQLite3::SQLException: tags_resources.resource_id may not be NULL:
UPDATE tags_resources SET resource_id = NULL WHERE (resource_id = 7
AND id IN (9))

---------------------------------------------------------------------------------------------------

I'm totally at a loss here - why is the generated SQL setting
resource_id = NULL in the first place?!

martin
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2006-01-22 02:39
(Received via mailing list)
On 1/22/06, Martin DeMello <martindemello@gmail.com> wrote:
> Setup as follows:

Oops - and the table structure:

CREATE TABLE 'tags' (
  'id' INTEGER PRIMARY KEY NOT NULL,
  'name' VARCHAR(80) DEFAULT NULL
);

CREATE TABLE 'tags_resources' (
  'id' INTEGER PRIMARY KEY NOT NULL,
  'tag_id' INTEGER NOT NULL,
  'resource_id' INTEGER NOT NULL,
  'user_id' INTEGER NOT NULL
);
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2006-01-22 19:48
(Received via mailing list)
Anyone? This has me totally stumped, and I can't even tell if it's a
problem with acts_as_taggable or with my code. Here's the stack trace:
as far as I can tell, it should simply be doing a DELETE FROM
tags_resources WHERE id in (7,8) - there should never be any occasion
to modify a record in the join table when untagging a resource.

------------------------------------------------------------------------------------------------

 ActiveRecord::StatementInvalid in Tagedit#delete_tag

SQLite3::SQLException: tags_resources.resource_id may not be NULL:
UPDATE tags_resources SET resource_id = NULL WHERE (resource_id = 6
AND id IN (7,8))

RAILS_ROOT: config/..
Application Trace | Framework Trace | Full Trace

/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/abstract_adapter.rb:88:in
`log'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/sqlite_adapter.rb:133:in
`execute'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/sqlite_adapter.rb:137:in
`update'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:486:in
`update_all'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/has_many_association.rb:154:in
`delete_records'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:52:in
`delete'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in
`transaction'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:91:in
`transaction'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:118:in
`transaction'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:50:in
`delete'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:39:in
`delete_all'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:66:in
`clear'
/usr/lib/ruby/gems/1.8/gems/acts_as_taggable-1.0.4/lib/taggable.rb:385:in
`tag'
./config/../app/controllers/tagedit_controller.rb:24:in `delete_tag'
./config/../app/controllers/tagedit_controller.rb:21:in `delete_tag'
This topic is locked and can not be replied to.