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

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.

    • as a subset to this, I see the “Stuff” controller is “class
      StuffController < ApplicationController”. Do I want to set
      up something like “class MyStuffController < Stuff
      Controller”. Is this possible?
  • 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!

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.

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”?

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.

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:

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==============================