# Working with a subset (users) data in a common model containing all user data

**URL:** <https://www.ruby-forum.com/t/working-with-a-subset-users-data-in-a-common-model-containing-all-user-data/182109>\
**Category:** Rails\
**Created:** [January 22, 2010, 7:12pm UTC](https://www.ruby-forum.com/t/working-with-a-subset-users-data-in-a-common-model-containing-all-user-data/182109 "2010-01-22T19:12:49Z")\
**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 22, 2010, 7:12pm UTC](https://www.ruby-forum.com/t/working-with-a-subset-users-data-in-a-common-model-containing-all-user-data/182109/1 "2010-01-22T19:12:49Z")

</div>

I have a learning application “Projects” set up with nested “Tasks”.  
I am now trying to use authlogic and set up UI’s so that after a user  
logs in, the user can go to a UI of “My Projects” and then do all the  
CRUD they need to do on their projects, but the user will only have  
Read access to projects that are not theirs (accessible through a  
different link). All users will have their Projects in the same data  
model (or that’s what I’m attempting to do). I created a “myprojects”  
controller, copied over the project controller code & have been trying  
to work with “current\_user.projects”, but I’m getting an error since  
the Project is an undefined method (see line three of Myprojects  
contoller below for error).

How does one do this? Is there a way to pass the Project Controller &  
Views the “current\_user.projects” data to re-use the Project  
Controller & Views? I know I’ll have to put condition logic in to  
display or not display the ‘Edit’ paths, I’m just buggered on how to  
pass the subset of data if this is the way to go. Do I need the  
Myprojects controller or am I barking up a wrong tree? Do named  
routes come into play here? Enquiring minds want to know as a famous  
tabloid once advertised…

Thanks in advance!!

Following is my code:  
--------------------Project controller--------------------  
class ProjectsController \< ApplicationController  
def index  
@projects = Project.all

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @projects }
end

```

end

def show  
@project = Project.find(params[:id])

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @projects }
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 = 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 = Project.find(params[:id])  
@project.destroy  
flash[:notice] = ‘Project was successfully deleted.’  
respond\_to do |format|  
format.html { redirect\_to(projects\_url) }  
format.xml { head :ok }  
end  
end  
end

--------------------Myprojects controller--------------------  
class MyprojectsController \< ApplicationController  
def index  
@myprojects = current\_user.Project.all #\<== The ‘Project’  
# reference here is getting an undefined method error

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @myprojects }
end

```

end

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

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @myprojects }
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 = current\_user.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

--------------------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...](https://github.com/binarylogic/authlogic_example)**
>
> An example rails app using the Authlogic authentication library - GitHub - binarylogic/authlogic\_example: An example rails app using the Authlogic authentication library

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

go there for now…  
acts\_as\_authentic

has\_many :projects  
end
