Acts_as_taggable: weird SQL problem with untagging


#1

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


#2

On 1/22/06, Martin DeMello removed_email_address@domain.invalid 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
);


#3

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:inexecute’
/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:inupdate_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:indelete’
/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:intransaction’
/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:indelete’
/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:inclear’
/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:indelete_tag’
./config/…/app/controllers/tagedit_controller.rb:21:in `delete_tag’