I''m trying to add per-user tags to a simple app built atop the
tiny_file example code, as follows:
-------------------------------------------------------------------------------------------------
sqlite> .schema
CREATE TABLE resources (
id INTEGER PRIMARY KEY,
filename VARCHAR(255),
user_id integer
);
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
);
CREATE TABLE ''users'' (
''id'' INTEGER PRIMARY KEY NOT NULL,
''login'' VARCHAR(80) DEFAULT NULL,
''password'' VARCHAR(40) DEFAULT NULL
);
-------------------------------------------------------------------------------------------------
class Resource < ActiveRecord::Base
belongs_to :user
validates_presence_of :filename
validates_uniqueness_of :filename, :message => "already exists, try
uploading another file or deleting first."
acts_as_taggable :join_class_name => ''TagResource''
-------------------------------------------------------------------------------------------------
class TagResource
belongs_to :user
end
-------------------------------------------------------------------------------------------------
class ResourcesController < ApplicationController
before_filter :login_required
def create
user = @session[:user]
@resource = Resource.new(params[:resource])
@resource.user = user
@resource.tag "document", :attributes => {:user_id =>
user.id}
return render :action => ''new'' unless request.method ==
:post and
@resource.save
flash[:notice] = "File successfully uploaded to
<strong>#{@resource.filename}</strong>."
redirect_to :action => ''index''
end
-------------------------------------------------------------------------------------------------
View:
select a file to upload
<%= file_field ''resource'', ''file''
%></p>
<%= text_field ''resource'', ''tag_names''
%></p>
-------------------------------------------------------------------------------------------------
The problem is when I try to upload a file, it complains that I''m
trying to insert a null user_id
tags_resources.user_id may not be NULL: INSERT INTO tags_resources
(''tag_id'', ''resource_id'',
''user_id'') VALUES(1, 5, NULL)
Any idea what I''m doing wrong?
martin