# Authlogic integration: How save user\_id to associated table when user is logged in?

**URL:** <https://www.ruby-forum.com/t/authlogic-integration-how-save-user-id-to-associated-table-when-user-is-logged-in/182607>\
**Category:** Rails\
**Created:** [January 30, 2010, 10:40pm UTC](https://www.ruby-forum.com/t/authlogic-integration-how-save-user-id-to-associated-table-when-user-is-logged-in/182607 "2010-01-30T22:40:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Souschef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3d9bf3/32.png) [@Souschef](https://www.ruby-forum.com/u/Souschef)\
**Post date:** [January 30, 2010, 10:40pm UTC](https://www.ruby-forum.com/t/authlogic-integration-how-save-user-id-to-associated-table-when-user-is-logged-in/182607/1 "2010-01-30T22:40:02Z")

</div>

Hello! Total newbie question (new to rails, very new to authlogic).  
I am attempting to save the user\_id to my projects table when a user  
is logged in. I have a number of sample projects in my database and I  
manually associated the projects to different users. I created a “my  
projects” controller that accurately retrieves from the db the  
projects associated with the logged in user. The issue I have is  
when I create a new project while a user is logged in: The user\_id is  
not saved to the projects table (the project is saved, just not the  
associated user-id). The Read, Update, and Delete functions work fine  
from the ‘my projects’ controller.

Below are my models & controllers. I’ve changed the ‘create’ step in  
the controller to be a number of things, but nothing seems to work.

Should I be associating ‘projects’ to the ‘UserSession’ instead (model  
also included below, but currently empty)? Or do I need to declare  
some sort of logic that saves user\_id when a user is actually logged  
in? Or do I need to create a new route that points to a ‘project save’  
that also saves the user\_id?

Thanks in advance for input!

--------------------Project model--------------------  
class Project \< ActiveRecord::Base  
validates\_presence\_of :name

# allow ordering of tasks by step\_number

has\_many :tasks, :dependent =\> :destroy, :order =\> ‘step\_number ASC’  
accepts\_nested\_attributes\_for :tasks, :reject\_if =\> lambda { |a|  
a.values.all?(&:blank?) }, :allow\_destroy =\> true

def task\_attributes=(task\_attributes)  
task\_attributes.each do |attributes|  
tasks.build(attributes)  
end  
end

# Following statements tie Projects to users

belongs\_to :user

end

--------------------User model--------------------  
class User \< ActiveRecord::Base

# following line commented out. Came from authlogic, but not sure

# what it means…

# attr\_accessible :username, :email, :password

# Added following line from railscast demo. Note:

# [GitHub - binarylogic/authlogic\_example: An example rails app using the Authlogic authentication library](http://github.com/binarylogic/authlogic_example)

# has an optional block for passing other config options, but didn’t

# go there for now…

acts\_as\_authentic

has\_many :projects  
end

------------------User Session model---------------  
class UserSession \< Authlogic::Session::Base  
end

----------------Myprojects controller----------------  
class MyprojectController \< ApplicationController  
def index  
@projects = current\_user.projects.all

respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @projects }  
# render\_component :controller =\> “projectcontroller”, :action  
=\> “index”, :params =\> { :projects =\> @projects }

end  
end

def show  
@project = current\_user.projects.find(params[:id])

respond\_to do |format|  
#format.html # show.html.erb  
# format.xml { render :xml =\> @myprojects }

```
 format.xml { render( :template=> 'project/show' ) }

```

end  
end

def new  
@project = Project.new

respond\_to do |format|  
format.html # new.html.erb  
format.xml { render :xml =\> @project }  
end  
end

def edit  
@project = project.find(params[:id])  
end

def create  
@project = Project.new(params[:project])

respond\_to do |format|  
if @project.save  
flash[:notice] = ‘Project was successfully created.’  
format.html { redirect\_to(@project) }  
format.xml { render :xml =\> @project, :status  
=\> :created, :location =\> @project }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @project.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

def update  
@project = current\_user.Project.find(params[:id])

respond\_to do |format|  
if @project.update\_attributes(params[:project])  
flash[:notice] = ‘Project was successfully updated.’  
format.html { redirect\_to(@project) }  
format.xml { head :ok }  
else  
format.html { render :action =\> “edit” }  
format.xml { render :xml =\> @project.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

def destroy  
@project = current\_user.project.find(params[:id])  
@project.destroy  
flash[:notice] = ‘Project was successfully deleted.’  
respond\_to do |format|  
format.html { redirect\_to(myprojects\_url) }  
format.xml { head :ok }  
end  
end  
end
