If You want to extend the user to Your own needs You can do it in the
following way :
Suppose You own user - class in Your system has the name Puser.
- scaffold a puser and modify it accordingly or build it up manually
In this example a user has an aditional attribute (skill) and is
related to many projects (n to M relation)
Here the creation of the table pusers :
def self.up
create_table :pusers do |t|
t.text :skills
#important to link to goldberg user
t.integer :user_id
t.timestamps
end
create_table :puser_project do |t|
t.integer :puser_ids
t.integer :project_id
end
end
- add to environment.rb in dir config :
require "#{File.dirname(FILE)}/../app/models/puser"
- modify the puser.rb - model
## Here You extend the existing class in goldberg
Goldberg::User.class_eval do
has_one :puser, :dependent => :destroy
end
- add a field in Your puser called user_id which is the foreign key to
the goldberg user.
class Puser < ActiveRecord::Base
has_and_belongs_to_many :projects
##do not declare a custom foreignkey if You do You have to change the
goldbergtables (we should not touch
belongs_to :user, :class_name => 'Goldberg::User'
# other associations
# example: has_many :posts
end
Goldberg has some validations in its model. So if You create your own
user form with own (additional) informations You have to take care of
at least the name email role_id and password (fieldname
clear_password).
To explain (and to test) I leave out the view (which You can make
yourself according your need) I show You with direct variables :
puser_controller.rb
def create
@puser = Puser.new()
user = params[:user]
us=Goldberg::User.find(:first, :conditions => ["email =?",user
[:email]])
if us != nil
puts "User existing"
end
ur = Goldberg::User.new
#set goldberg "must have" data
ur.name = user[:name]
ur.clear_password = "heinz"
ur.email = "
erh...@kargers.org"
ur.role_id=1
#creates the link from the goldberg-user to your own user with
this command
ur.create_puser
ur.puser = @puser
ur.puser.skills = "Ruby on Rails"
ok = ur.save
respond_to do |format|
if ok
flash[:notice] = 'Puser was successfully created.'
format.html { redirect_to(@puser) }
format.xml { render :xml => @puser, :status
=> :created, :location => @puser }
else
format.html { render :action => "new" }
format.xml { render :xml => @puser.errors, :status
=> :unprocessable_entity }
end
end
end
As You can see from the sql in both tables the creation took place.
Goldberg::User Create (0.3ms) INSERT INTO "goldberg_users" ("name",
"password_expired", "start_path", "role_id", "password_salt",
"fullname", "self_reg_confirmation_required", "password_changed_at",
"password", "email", "confirmation_key") VALUES('et', NULL, NULL, 1,
'-6116462680.260300681189902', NULL, NULL, NULL,
'6530fdb37f4e221f1ecd9b7041de2f6d2bc9e1d4', '
erh...@kargers.org',
NULL)
Puser Create (0.2ms) INSERT INTO "pusers" ("updated_at", "skills",
"user_id", "created_at") VALUES('2009-07-01 17:07:15', 'Ruby on
Rails', 14, '2009-07-01 17:07:15')
Redirected to
http://localhost:3000/pusers/3