# Newbie question - how set up working with a subset of data (e.g. user's data)?

**URL:** <https://www.ruby-forum.com/t/newbie-question-how-set-up-working-with-a-subset-of-data-e-g-users-data/181648>\
**Category:** Rails\
**Created:** [January 15, 2010, 7:12am UTC](https://www.ruby-forum.com/t/newbie-question-how-set-up-working-with-a-subset-of-data-e-g-users-data/181648 "2010-01-15T07:12:10Z")\
**Posts on this page:** 5\
**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 15, 2010, 7:12am UTC](https://www.ruby-forum.com/t/newbie-question-how-set-up-working-with-a-subset-of-data-e-g-users-data/181648/1 "2010-01-15T07:12:10Z")

</div>

I’m new to rails & am building out an application to learn from. From  
a conceptual perspective, how would I go about working with a subset  
of data? For example, say I have an application with basic CRUD  
operations on “Stuff”. I’ve implemented the restful\_authentication  
plugin and am now struggling with how would I deal with setting up  
CRUD for “MyStuff” (i.e. Stuff associated with a particular user ID).  
What’s the best approach? Any good examples/tutorials out there?

Following are thoughts I’m considering:

- Recreate the controller & views for “Stuff”, but just using the  
subset of data associated with the user’s ID.

- I’ve read on various the web pages about filtering, but not sure I  
understand it fully. Can this be used?  
Really I’m just working with a subset of “Stuff”, and I’d think I  
should be able to re-use the controller, views, etc.  
by just passing it a different data set, but then maybe this is a  
naive perspective.

- Is this an inheritance concept?

Thanks in advance for your guidance!

---

<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 15, 2010, 10:24am UTC](https://www.ruby-forum.com/t/newbie-question-how-set-up-working-with-a-subset-of-data-e-g-users-data/181648/2 "2010-01-15T10:24:44Z")

</div>

Stuff has\_many my\_stuff

Then any individual Stuff will expose .my\_stuff method. You will have  
to create the CRUD operations for the MyStuff, that will contain  
stuff\_id field for the association.

---

<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 15, 2010, 5:20pm UTC](https://www.ruby-forum.com/t/newbie-question-how-set-up-working-with-a-subset-of-data-e-g-users-data/181648/3 "2010-01-15T17:20:57Z")

</div>

> Stuff has\_many my\_stuff  
> So in the model of “Stuff” declare that Stuff has\_many my\_stuff. The  
> goal of this is to reuse the data in the Stuff table based on a user  
> ID, not set up a separate table, yes?

> Then any individual Stuff will expose .my\_stuff method. You will have  
> to create the CRUD operations for the MyStuff, that will contain  
> stuff\_id field for the association.  
> I create the CRUD for “MyStuff” w/ the user ID & stuff ID (versus just  
> the stuff ID in the “Stuff” CRUDs). Do I need a new controller for  
> “MyStuff”?

---

<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 15, 2010, 5:33pm UTC](https://www.ruby-forum.com/t/newbie-question-how-set-up-working-with-a-subset-of-data-e-g-users-data/181648/4 "2010-01-15T17:33:15Z")

</div>

Let me get this straight. You have users. Users have Stuff. You want a  
page,  
say, where a given user can see just their Stuff, and nobody else’s?

Well, you’d want to make your User have\_many :stuffs, and your Stuffs  
belong\_to :user . This’ll require a user\_id column in your Stuffs table.

Then, finding the Stuff for a certain user is as easy as  
“User.find(id).stuffs” . I forget if restful\_authentication has a  
current\_user helper, I use AuthLogic, and so I’d just say  
“current\_user.stuffs”

But you probably don’t want to make a whole second set of controllers  
just  
for “MyStuffs”, unless some stuffs don’t belong to a user, but they can  
still modify them. It’s hard to understand the relationship with such  
generic terms.

If all stuffs belong to a user, then you probably want to make Stuffs a  
nested resouce of users. See the Railscast on the topic, it might be  
what  
you’re looking for.

> **[\#139 Nested Resources - RailsCasts](http://railscasts.com/episodes/139-nested-resources)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

---

<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, 5:44am UTC](https://www.ruby-forum.com/t/newbie-question-how-set-up-working-with-a-subset-of-data-e-g-users-data/181648/5 "2010-01-22T05:44:26Z")

</div>

OK! Removed restful\_authentication & installed/integrated the  
authlogic gem. I have everything up according to railscast and now I  
am trying to integrate the authlogic functions.

To be more specific than I originally was, I have 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, then 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. 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.

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 #\<-------- ‘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

==================== END 1/21/10 NOTE==============================
